Replies: 1 comment 2 replies
-
The error "Single '}' encountered in format string" is typically caused by unescaped curly braces in your format strings. In your code, the Here's the corrected code snippet with the necessary changes: from langchain_core.prompts import PromptTemplate
from langchain_core.tools import Tool
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent
prompt = PromptTemplate.from_template("{query}")
llm1 = ChatOpenAI(model="gpt-4o-mini", temperature=0.5)
llm_chain = LLMChain(prompt=prompt, llm=llm1)
retriever = vector_store.as_retriever(search_kwargs={"k": 4})
relevant_document = retriever.get_relevant_documents(query)
context_text = "\n\n---\n\n".join([doc.page_content for doc in relevant_document])
tools = [
Tool(
name="Search summaries",
func=new_project_qa,
description="useful for when you need to answer questions. Input should be a fully formed question.",
returnDirect=True,
),
Tool(
name="llm_search",
description="Search llm for recent results.",
func=llm_chain.run,
),
]
PREFIX = f"""You are a helpful assistant. You have to give answers at any cost. For answering, first use the given context to answer. If you can't find the answer, then answer from your knowledge.
You have access to the following tools:
Context_text: {context_text}"""
FORMAT_INSTRUCTIONS = """Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: take action from "Search summaries" after this use other tools, should be one of [{tool_names}], first find the answer from the context, if you can't find the answer in data then use other tools i.e. "llm_search"
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer and I have to provide the answer in markdown.
The answer may contain a heading but not always.
Use heading: "**.......**"
Final Answer: the final answer to the original input question. Structure your answer with valid headings if required, and use points or paragraphs as needed.
"""
SUFFIX = """Begin!
Question: {input}
Thought: {agent_scratchpad}"""
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1)
agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=False,
max_iterations=3,
handle_parsing_errors=True,
return_source_documents=True,
return_intermediate_steps=True,
early_stopping_method="generate",
agent_kwargs={
"prefix": PREFIX,
"format_instructions": FORMAT_INSTRUCTIONS,
"suffix": SUFFIX,
},
) In the corrected code, the curly braces around |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Getting below error while running the above mentioned code
Traceback (most recent call last):
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/chatbot/views.py", line 559, in GetChatResponse
chat_response = chat_langchain(qa_chains, query, not_uuid)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/accounts/common_langcain_qa.py", line 516, in chat_langchain
agent = initialize_agent(
^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/langchain_core/_api/deprecation.py", line 168, in warning_emitting_wrapper
return wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/langchain/agents/initialize.py", line 75, in initialize_agent
agent_obj = agent_cls.from_llm_and_tools(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/langchain/agents/mrkl/base.py", line 134, in from_llm_and_tools
prompt = cls.create_prompt(
^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/langchain/agents/mrkl/base.py", line 104, in create_prompt
return PromptTemplate.from_template(template)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/langchain_core/prompts/prompt.py", line 282, in from_template
input_variables = get_template_variables(template, template_format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deepak/CustomBot/.venv/lib/python3.12/site-packages/langchain_core/prompts/string.py", line 249, in get_template_variables
input_variables = {
^
ValueError: Single '}' encountered in format string
System Info
langchain==0.2.11
langchain-anthropic==0.1.22
langchain-community==0.2.10
langchain-core==0.2.26
langchain-openai==0.0.5
langchain-qdrant==0.1.3
langchain-text-splitters==0.2.2
langchain-weaviate==0.0.2
langchainhub==0.1.14
Linux
Python 3.12.4
Beta Was this translation helpful? Give feedback.
All reactions