Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain import OpenAI
|
| 3 |
+
from langchain.chat_models import ChatOpenAI
|
| 4 |
+
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 5 |
+
|
| 6 |
+
turbo_llm = ChatOpenAI(
|
| 7 |
+
temperature=0,
|
| 8 |
+
model_name='gpt-3.5-turbo'
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
from langchain.tools import DuckDuckGoSearchTool
|
| 12 |
+
from langchain.agents import Tool
|
| 13 |
+
from langchain.tools import BaseTool
|
| 14 |
+
|
| 15 |
+
search = DuckDuckGoSearchTool()
|
| 16 |
+
# defining a single tool
|
| 17 |
+
tools = [
|
| 18 |
+
Tool(
|
| 19 |
+
name = "search",
|
| 20 |
+
func=search.run,
|
| 21 |
+
description="useful for when you need to answer questions about current events. You should ask targeted questions"
|
| 22 |
+
)
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
def meaning_of_life(input=""):
|
| 26 |
+
return 'The meaning of life is 42 if rounded but is actually 42.17658'
|
| 27 |
+
life_tool = Tool(
|
| 28 |
+
name='Meaning of Life',
|
| 29 |
+
func= meaning_of_life,
|
| 30 |
+
description="Useful for when you need to answer questions about the meaning of life. input should be MOL "
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
import random
|
| 34 |
+
def random_num(input=""):
|
| 35 |
+
return random.randint(0,5)
|
| 36 |
+
#random_num()
|
| 37 |
+
random_tool = Tool(
|
| 38 |
+
name='Random number',
|
| 39 |
+
func= random_num,
|
| 40 |
+
description="Useful for when you need to get a random number. input should be 'random'"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
from langchain.agents import initialize_agent
|
| 44 |
+
|
| 45 |
+
tools = [search, random_tool, life_tool]
|
| 46 |
+
|
| 47 |
+
# conversational agent memory
|
| 48 |
+
memory = ConversationBufferWindowMemory(
|
| 49 |
+
memory_key='chat_history',
|
| 50 |
+
k=3,
|
| 51 |
+
return_messages=True
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# create our agent
|
| 55 |
+
conversational_agent = initialize_agent(
|
| 56 |
+
agent='chat-conversational-react-description',
|
| 57 |
+
tools=tools,
|
| 58 |
+
llm=turbo_llm,
|
| 59 |
+
verbose=True,
|
| 60 |
+
max_iterations=3,
|
| 61 |
+
early_stopping_method='generate',
|
| 62 |
+
memory=memory
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
ai_res=conversational_agent("What time is it in Beijing China?")
|
| 66 |
+
print(ai_res)
|