-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path004-tutorial.py
70 lines (55 loc) · 1.98 KB
/
004-tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 004-tutorial.py
# LangChain Tutorial: Using DuckDuckGo Search Tool
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, PromptTemplate
from langchain_core.runnables import RunnableSequence
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.agents import create_react_agent, AgentExecutor
# Load environment variables
load_dotenv()
# Initialize our language model
# Note: 'gpt-4o-mini' is not an official OpenAI model name. This may cause an error.
llm = ChatOpenAI(model="gpt-4o-mini")
# Create the DuckDuckGo search tool
search = DuckDuckGoSearchRun()
# Create a list with the DuckDuckGo search tool
tools = [
DuckDuckGoSearchRun(name="Web Search")
]
# Function to load prompt from file
def load_prompt_template(file_path):
with open(file_path, 'r') as file:
return file.read()
# Load the React prompt template from file
react_prompt_template = load_prompt_template('tutorial.react-prompt.md')
# Create the PromptTemplate
react_prompt = PromptTemplate.from_template(react_prompt_template)
# Create the agent
agent = create_react_agent(llm, tools, react_prompt)
# Create an agent executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Create a prompt template for the main query
prompt = ChatPromptTemplate.from_template(
"Answer the following question using the most up-to-date information you can find: {question}"
)
# Create our RunnableSequence
sequence = RunnableSequence(
prompt,
lambda prompt_result: agent_executor.invoke({
"input": prompt_result.to_messages()[0].content,
"tools": tools,
"tool_names": ", ".join([tool.name for tool in tools]),
"agent_scratchpad": ""
})
)
def main():
# Use the sequence
result = sequence.invoke({
"question": "What are the latest developments in artificial intelligence?"
})
print("Answer:")
print(result['output'])
if __name__ == "__main__":
main()