uyen13 commited on
Commit
519ae16
·
verified ·
1 Parent(s): 14c12a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -37
app.py CHANGED
@@ -1,42 +1,21 @@
1
- import json
2
- from langchain.chains import SimpleChatChain
3
- from langchain.prompts import PromptTemplate
4
- from langchain.llms import OpenAI
5
 
6
  # Define your API key for OpenAI or any other LLM provider
7
- openai_api_key = 'sk-nAqoChT9cmkAxALwMLdWT3BIbkFJcNHsH5Z5LN2ixPcDAopT'
 
8
 
9
- # Initialize the LLM
10
- llm = OpenAI(api_key=openai_api_key)
11
 
12
- # Define a simple chat chain with a prompt template
13
- chat_chain = SimpleChatChain(
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
- # Create the response JSON
30
- response_json = json.dumps({'response': response})
31
- return response_json
32
-
33
- # Example usage
34
- if __name__ == "__main__":
35
- # Simulate a JSON input from the user
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?"})