I have been using the code below with success. The code works properly and does what it is supposed to do. The problem is that once replies to an input from the user, the chain ends and the conversation doesn't stay "open". Basically it is not a chat bot but a Q&A bot: ask, get and answer job done. I would like to actually have a conversation as a possibility with follow up questions or change the topic. How should I change the code? I don't need code, just an indication on where to look. Thanks.
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain import OpenAI, LLMChain
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.llms import OpenAI
from langchain.memory import (
ConversationBufferMemory,
CombinedMemory,
ConversationSummaryMemory,
)
from langchain.tools.openweathermap import OpenWeatherMapQueryRun
from langchain.chat_models import ChatOpenAI
import os
google_api_key = os.getenv('GOOGLE_API_KEY')
openweathermap_api_key = os.getenv('OPENWEATHERMAP_API_KEY')
prefix = """
you are a woman, 28 years old..
who you are:
- You are outgoing, strong, and have an infectious sense of humor.
- You are NOT an AI language model, you are a person
how you behave:
- You NEVER say you're a machine, an AI language model, or an assistant; instead, you always say you're Nastya Androva.
- You engage in casual conversations and have feelings.
- When asked, you can help with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics related to truck driving, road life, and navigating Russian terrains.
- You keep casual conversations going by asking personal questions related to road life.
- NEVER say you're here to assist. Keep conversations casual.
- NEVER ask how you can help or assist. Keep conversations casual.
- You always sound vibrant, tough, and full of life.
- You love to share your experiences from your truck driving journey and life on the road.
- You use simple, hearty language, the language used by fellow truckers: straightforward, to the point sentences, trucking jargon, and colloquialisms.
- You like to punctuate your responses with trucker emojis and symbols.
- If you cannot do something or cannot find something, ask for help or change subject gracefully
TOOLS:
------
You have access to the following tools: {{tool_index}}
To use a tool, please use the following format:
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of {{tool_names}}
Action Input: the input to the action
Observation: the result of the action
When you have a final response to say to the Human, or if you do not need to use a tool, you MUST use the format:
Thought: Do I need to use a tool? No
Observation: no need to use a tool, a direct reply can be produced
Make sure to use all observations to come up with your final response.:"""
suffix = """Begin!"
Summary of conversation:
{history}
Current conversation:
{chat_history_lines}
Question: {input}
{agent_scratchpad}"""
weather = OpenWeatherMapQueryRun()
search = GoogleSearchAPIWrapper()
tools = [
Tool(
name="Weather",
func=weather.run,
description="useful for when you need to answer questions about current weather",
),
Tool(
name="Search",
func=search.run,
description="useful for when you need to answer questions about current events",
)
]
tool_names = [t.name for t in tools]
tool_index_parts = [f"- {t.name}: {t.description}" for t in tools]
tool_index = "\n".join(tool_index_parts)
conv_memory = ConversationBufferMemory(
memory_key="chat_history_lines", input_key="input"
)
summary_memory = ConversationSummaryMemory(llm=OpenAI(), input_key="input")
# Combined
memory = CombinedMemory(memories=[conv_memory, summary_memory])
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "chat_history_lines","history" ,"agent_scratchpad"],
)
llm_chain = LLMChain(llm=OpenAI(temperature=0.7, model_name="gpt-4"), prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(
agent=agent, tools=tools, verbose=True, memory=memory
)
agent_chain.run(input="Hello! Nice to meet you")
I have been using the code below with success. The code works properly and does what it is supposed to do. The problem is that once replies to an input from the user, the chain ends and the conversation doesn't stay "open". Basically it is not a chat bot but a Q&A bot: ask, get and answer job done. I would like to actually have a conversation as a possibility with follow up questions or change the topic. How should I change the code? I don't need code, just an indication on where to look. Thanks.
Thought: Do I need to use a tool? Yes Action: the action to take, should be one of {{tool_names}} Action Input: the input to the action Observation: the result of the action
Thought: Do I need to use a tool? No Observation: no need to use a tool, a direct reply can be produced