crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

JSON formatted Agent output

Open CryptoPensioner opened this issue 5 months ago • 2 comments

Is there a way to use model_kwargs, output_format, or anything like this to get OpenAIs JSON mode? I don't want to format data after it comes back, I want it specifically provided as JSON.

Example code from OpenAI docu: https://platform.openai.com/docs/guides/text-generation/json-mode

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
  model="gpt-3.5-turbo-1106",
  response_format={ "type": "json_object" },
  messages=[
    {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
    {"role": "user", "content": "Who won the world series in 2020?"}
  ]
)
print(response.choices[0].message.content)

Is there a way to utilize that with CrewAI Agents? I'm using langchain_openai and gpt-4-1106-preview as llm model.

CryptoPensioner avatar Jan 19 '24 21:01 CryptoPensioner

In the mean time, you can ask explicitly to return JSON and provide an example. That already works 99% of the time without the official JSON flag.

japborst avatar Jan 21 '24 22:01 japborst

Perhaps we can just add something like this:

chat = ChatOpenAI(model="gpt-3.5-turbo-1106").bind(
    response_format={"type": "json_object"}
)

Somewhere around here.

jadechip avatar Jan 22 '24 09:01 jadechip