-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_base.txt
51 lines (41 loc) · 2.55 KB
/
code_base.txt
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
from langchain.agents import Tool
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain import OpenAI
from langchain.utilities import SerpAPIWrapper
from langchain.agents import initialize_agent
import os
import json
# Step 1: Please add environment api keys for SerpAPI and OpenAI
os.environ["OPENAI_API_KEY"] = ""
os.environ["SERPAPI_API_KEY"] = ""
# Step 2: Create a JSON 'weather_agent.json' with the following metadata
weather_agent = {
"load_from_llm_and_tools": True,
"_type": "conversational-react-description",
"prefix": "Assistant is a large language model trained for forecasting weather.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives.\nAssistant should only obtain knowledge and take actions from using tools and never on your own.\nTOOLS:\n------\n\nAssistant has access to the following tools: ",
"suffix": "Please make decisions based on the following policy: \n- If the user is asking for a weather forecast use the Weather Forecast tool\n- If the user does not provide a location, ask before checking for weather\n- Apologize if the user is angry or showing frustration\n- Answer with a friendly and professional tone.\n- Always end a response with a follow up question like 'what else can i help you with?', unless the user shows gratitude.\nBegin!\n\nPrevious conversation history:\n{chat_history}\n\nNew input: {input}\n{agent_scratchpad}",
"ai_prefix": "AI Agent",
"human_prefix": "Human"
}
# Define the path to the local file where you want to save the JSON data
file_path = "/content/weather_agent.json"
# Write the JSON data to the file
with open(file_path, "w") as json_file:
json.dump(weather_agent, json_file, indent=2)
print(f"JSON data has been saved to {file_path}")
search = SerpAPIWrapper()
tools = [
Tool(
name="Weather Forecaster",
func=search.run,
description="useful for when you need to answer questions about current and future weather forecast"
),
]
memory = ConversationBufferMemory(memory_key="chat_history")
llm = OpenAI(temperature=0)
agent_chain = initialize_agent(tools, llm=llm, verbose=True,
agent_path="/content/weather_agent.json",
memory=memory)
while True:
query = input(">> ")
agent_chain.run(input=query)