I have adapted this notebook from various tutorials:
https://colab.research.google.com/drive/1tAa5QUWplKRbYsUROZ1tmPiEZ4kY9sio?usp=sharing
I want to make this model into a tool and call the tool to parse human input as a node in langgraph:
class PlanItem(BaseModel):
step: str
tools: Optional[str] = []
data_sources: Optional[str] = []
sub_steps_needed: str
class Plan(BaseModel):
plan: List[PlanItem]
This function should create the agent for the node:
def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):
# Each worker node will be given a name and some tools.
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
system_prompt,
),
MessagesPlaceholder(variable_name="messages"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
agent = create_openai_tools_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
return executor
but it doesn’t recognise Plan as a BaseModel and I cannot convert it to a function I can pass as a tool.
Using pydantic.v1 I tried several approaches using PydanticOutputParser, PydanticOutputFunctionsParser and PydanticToolsParser. I also tried using convert_pydantic_to_openai_function:
output_parser = PydanticOutputParser(pydantic_object=Plan)
output_parser = PydanticOutputFunctionsParser(pydantic_schema=Plan)
output_parser = PydanticToolsParser(tools=[Plan])
output_parser = convert_pydantic_to_openai_function(Plan) output_parser['description'] = "Parses data into Plan" output_parser['title'] = "Plan Parser"
I seem to get errors which are variations on:
ValueError: Unsupported function
pydantic_schema=<class '__main__.Plan'>
Functions must be passed in as Dict, pydantic.BaseModel, or Callable. If they're a dict they must either be in OpenAI function format or valid JSON schema with top-level 'title' and 'description' keys.
There are tutorials on generally adding the parser as a tool in langchain, but I want the parser to only be invoked at a particular node.
Can anybody show me how to do this?
Melanie Thompson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.