llama_index: [Bug]: Error using pydantic response with chat engine

Bug Description

I’m trying to get pydantic response with chat engine but it threw errors. i tried different versions of llamaindex but none of them worked.

Version

0.10.36

Steps to Reproduce

!pip install llama-index-vector-stores-pinecone==0.1.7 pinecone-client==3.2.2 llama-index-embeddings-openai !pip install llama-index==0.10.36

from llama_index.llms.openai import OpenAI as llama_index_openai import openai from llama_index.embeddings.openai import OpenAIEmbedding from pinecone import Pinecone from llama_index.vector_stores.pinecone import PineconeVectorStore from llama_index.core import ( VectorStoreIndex ) from typing import List from pydantic.v1 import BaseModel, Field

class Movie(BaseModel): “”“Object representing a single movie.”“”

name: str = Field(…, description=“Name of the movie.”) year: int = Field(…, description=“Year of the movie.”)

class Movies(BaseModel): “”“Object representing a list of movies.”“”

movies: List[Movie] = Field(…, description=“List of movies.”)

openai.api_key = “XXX” embed_model = OpenAIEmbedding(model=“text-embedding-3-small”)

llm = llama_index_openai(temperature=0.1, model=“gpt-4o”) pc = Pinecone(api_key=“XXX”) pinecone_index = pc.Index(“quick_start”) vector_store = PineconeVectorStore(pinecone_index=pinecone_index) loaded_index = VectorStoreIndex.from_vector_store(vector_store=vector_store, embed_model=embed_model) sllm = llm.as_structured_llm(Movies) query_engine = loaded_index.as_chat_engine( chat_mode=“context”, similarity_top_k=5, llm=sllm ) prompt = ‘’’ Please generate related movies to Titanic ‘’’ response = query_engine.chat(prompt)

Relevant Logs/Tracbacks

ValueError Traceback (most recent call last)
<ipython-input-12-8916b3b995ec> in <cell line: 1>()
----> 1 response = query_engine.chat(prompt)

7 frames
/usr/local/lib/python3.10/dist-packages/llama_index/core/instrumentation/dispatcher.py in wrapper(func, instance, args, kwargs)
 259 )
 260 try:
--> 261 result = func(*args, **kwargs)
 262 except BaseException as e:
 263 self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

/usr/local/lib/python3.10/dist-packages/llama_index/core/callbacks/utils.py in wrapper(self, *args, **kwargs)
 39 callback_manager = cast(CallbackManager, callback_manager)
 40 with callback_manager.as_trace(trace_id):
---> 41 return func(self, *args, **kwargs)
 42 
 43 @functools.wraps(func) # preserve signature, name, etc. of func

/usr/local/lib/python3.10/dist-packages/llama_index/core/chat_engine/condense_plus_context.py in chat(self, message, chat_history)
 287 
 288 # pass the context, system prompt and user message as chat to LLM to generate a response
--> 289 chat_response = self._llm.chat(chat_messages)
 290 assistant_message = chat_response.message
 291 self._memory.put(assistant_message)

/usr/local/lib/python3.10/dist-packages/llama_index/core/instrumentation/dispatcher.py in wrapper(func, instance, args, kwargs)
 259 )
 260 try:
--> 261 result = func(*args, **kwargs)
 262 except BaseException as e:
 263 self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

/usr/local/lib/python3.10/dist-packages/llama_index/core/llms/callbacks.py in wrapped_llm_chat(_self, messages, **kwargs)
 170 )
 171 try:
--> 172 f_return_val = f(_self, messages, **kwargs)
 173 except BaseException as e:
 174 callback_manager.on_event_end(

/usr/local/lib/python3.10/dist-packages/llama_index/core/llms/structured_llm.py in chat(self, messages, **kwargs)
 105 # the messages don't technically have any variables (they are already formatted)
 106 
--> 107 chat_prompt = ChatPromptTemplate(message_templates=_escape_json(messages))
 108 
 109 output = self.llm.structured_predict(

/usr/local/lib/python3.10/dist-packages/llama_index/core/prompts/base.py in __init__(self, message_templates, prompt_type, output_parser, metadata, template_var_mappings, function_mappings, **kwargs)
 247 template_vars = []
 248 for message_template in message_templates:
--> 249 template_vars.extend(get_template_vars(message_template.content or ""))
 250 
 251 super().__init__(

/usr/local/lib/python3.10/dist-packages/llama_index/core/prompts/utils.py in get_template_vars(template_str)
 10 formatter = Formatter()
 11 
---> 12 for _, variable_name, _, _ in formatter.parse(template_str):
 13 if variable_name:
 14 variables.append(variable_name)

ValueError: Single '}' encountered in format string

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

openai.api_key = “XXX” embed_model = OpenAIEmbedding(model=“text-embedding-3-small”)

llm = llama_index_openai(temperature=0.1, model=“gpt-4o”) pc = Pinecone(api_key=“XXX”) pinecone_index = pc.Index(“get_started”) vector_store = PineconeVectorStore(pinecone_index=pinecone_index) loaded_index = VectorStoreIndex.from_vector_store(vector_store=vector_store, embed_model=embed_model) sllm = llm.as_structured_llm(AllExecSummary) query_engine = loaded_index.as_chat_engine( chat_mode=“context”, similarity_top_k=5, llm=sllm ) prompt = ‘’’ Provide executive summaries for project amber and project lina from last two weeks. The response of this instruction must be in JSON format. ‘’’ response = query_engine.chat(prompt)