camel icon indicating copy to clipboard operation
camel copied to clipboard

[Feature Request] Integrate structured output for agent

Open lightaime opened this issue 1 year ago • 2 comments

Required prerequisites

  • [X] I have searched the Issue Tracker and Discussions that this hasn't already been reported. (+1 or comment there if it has.)
  • [ ] Consider asking first in a Discussion.

Motivation

Inspired by the recent progress of LLM on structured output: https://simmering.dev/blog/structured_output/. I think it is imporant to support structured output for agents. Here is a rough thought on how this could be look like:

from typing import List, Literal
from pydantic import BaseModel, Field

from camel.agents import ChatAgent

class Entity(BaseModel):
    name: str = Field(description="name of the entity")
    label: Literal["PERSON", "ORGANIZATION", "LOCATION"]

class ExtractEntities(BaseModel):
    entities: List[Entity]

agent = ChatAgent(...)

out_response = agent.step(msg, response_schema=ExtractEntities)
content: ExtractEntities = out_response.msg.content  # This will need message to support different schema

This could be also helpful for the knowledge graph agent as well.

Solution

No response

Alternatives

No response

Additional context

No response

lightaime avatar May 27 '24 16:05 lightaime

Currently, there are three solutions and three different issues:

Add output schema in init chatagent; Add output schema as a tool into tools of chat agent; Add output schema as parameters in agent step func;

But now there are three problems:

for step 1 init chat agent requires us to customize the OpenAI output schema. However, there are many conflicts with our for original architecture, which requires significant modifications to our existing structure. We discussed this with Zecheng, and it looks very complex. for step 2 We need to convert Pydantic to a function and then add it as a function tool into chat agent's tools. for step 3 We need to modify the structure of the step and the openai model run function. Since we use the function call method to implement this, it requires specifying the tools parameter for the OpenAI run, and we also need to modify the chat agent step to help us output the response with the output schema.

raywhoelse avatar Jul 12 '24 12:07 raywhoelse

@lightaime @Wendong-Fan

raywhoelse avatar Jul 12 '24 12:07 raywhoelse

finished

Wendong-Fan avatar Oct 31 '24 06:10 Wendong-Fan