Spaces:
Runtime error
Runtime error
| import os | |
| from langchain import OpenAI | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.chains.conversation.memory import ConversationBufferWindowMemory | |
| turbo_llm = ChatOpenAI( | |
| temperature=0, | |
| model_name='gpt-3.5-turbo' | |
| ) | |
| from langchain.tools import DuckDuckGoSearchTool | |
| from langchain.agents import Tool | |
| from langchain.tools import BaseTool | |
| search = DuckDuckGoSearchTool() | |
| # defining a single tool | |
| tools = [ | |
| Tool( | |
| name = "search", | |
| func=search.run, | |
| description="useful for when you need to answer questions about current events. You should ask targeted questions" | |
| ) | |
| ] | |
| def meaning_of_life(input=""): | |
| return 'The meaning of life is 42 if rounded but is actually 42.17658' | |
| life_tool = Tool( | |
| name='Meaning of Life', | |
| func= meaning_of_life, | |
| description="Useful for when you need to answer questions about the meaning of life. input should be MOL " | |
| ) | |
| import random | |
| def random_num(input=""): | |
| return random.randint(0,5) | |
| #random_num() | |
| random_tool = Tool( | |
| name='Random number', | |
| func= random_num, | |
| description="Useful for when you need to get a random number. input should be 'random'" | |
| ) | |
| from langchain.agents import initialize_agent | |
| tools = [search, random_tool, life_tool] | |
| # conversational agent memory | |
| memory = ConversationBufferWindowMemory( | |
| memory_key='chat_history', | |
| k=3, | |
| return_messages=True | |
| ) | |
| # create our agent | |
| conversational_agent = initialize_agent( | |
| agent='chat-conversational-react-description', | |
| tools=tools, | |
| llm=turbo_llm, | |
| verbose=True, | |
| max_iterations=3, | |
| early_stopping_method='generate', | |
| memory=memory | |
| ) | |
| ai_res=conversational_agent("What time is it in Beijing China?") | |
| print(ai_res) | |