Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,21 @@
|
|
| 1 |
-
import
|
| 2 |
-
from langchain.
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
|
| 6 |
# Define your API key for OpenAI or any other LLM provider
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
llm = OpenAI(api_key=openai_api_key)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
llm=llm,
|
| 15 |
-
prompt_template=PromptTemplate(
|
| 16 |
-
input_variables=["user_input"],
|
| 17 |
-
template="User: {user_input}\nBot:"
|
| 18 |
-
)
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
def chat_with_json(input_json):
|
| 22 |
-
# Parse the input JSON
|
| 23 |
-
input_data = json.loads(input_json)
|
| 24 |
-
user_input = input_data.get('message', '')
|
| 25 |
-
|
| 26 |
-
# Generate a response using the chat chain
|
| 27 |
-
response = chat_chain.run(user_input)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
user_input_json = json.dumps({'message': 'Hello, how are you?'})
|
| 37 |
-
|
| 38 |
-
# Get the response from the chatbot
|
| 39 |
-
response_json = chat_with_json(user_input_json)
|
| 40 |
-
|
| 41 |
-
# Print the response JSON
|
| 42 |
-
print(response_json)
|
|
|
|
| 1 |
+
from langchain import hub
|
| 2 |
+
from langchain.agents import AgentExecutor, create_json_chat_agent
|
| 3 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
|
| 6 |
# Define your API key for OpenAI or any other LLM provider
|
| 7 |
+
OPENAI_API_KEY = 'sk-nAqoChT9cmkAxALwMLdWT3BIbkFJcNHsH5Z5LN2ixPcDAopT'
|
| 8 |
+
openai.api_key=OPENAI_API_KEY
|
| 9 |
|
| 10 |
+
tools = [TavilySearchResults(max_results=1)]
|
|
|
|
| 11 |
|
| 12 |
+
# Choose the LLM that will drive the agent
|
| 13 |
+
llm = ChatOpenAI(openai_api_key=openai.api_key,temperature=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Construct the JSON agent
|
| 16 |
+
agent = create_json_chat_agent(llm, tools, prompt)
|
| 17 |
+
# Create an agent executor by passing in the agent and tools
|
| 18 |
+
agent_executor = AgentExecutor(
|
| 19 |
+
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
|
| 20 |
+
)
|
| 21 |
+
agent_executor.invoke({"input": "what is LangChain?"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|