lobehub / lobe-chat

🤯 Lobe Chat - an open-source, modern-design AI chat framework. Supports Multi AI Providers( OpenAI / Claude 3 / Gemini / Ollama / Azure / DeepSeek), Knowledge Base (file upload / knowledge management / RAG ), Multi-Modals (Vision/TTS) and plugin system. One-click FREE deployment of your private ChatGPT/ Claude application.
https://chat-preview.lobehub.com
Other
43.98k stars 9.84k forks source link

[Docs] Feedback for `usage/tools-calling` #4560

Open twang2218 opened 3 hours ago

twang2218 commented 3 hours ago

📝 Reference

https://github.com/lobehub/lobe-chat/tree/main/docs/usage/tools-calling.zh-CN.mdx

🐛 Feedback

tools-calling除了这个应用外,还有一个应用形式,即「结构化输出」(Structured Output),如:

from typing import Optional
from pathlib import Path
from pydantic import BaseModel, Field
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

class Joke(BaseModel):
    '''Joke to tell user.'''
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(description="How funny the joke is, from 1 to 10")

model = ChatOpenAI(model="gpt-4o-mini")
model = model.with_structured_output(Joke)
prompt = ChatPromptTemplate.from_messages([
    ("system", "你是一个幽默大师"),
    ("user", "{text}")
])

在LangSmith中可以看到返回后会调用PydanticToolParser,从而确保了语言模型的输出符合指定结构。

在具体实现上,呼叫API的时候,tools中会给出指定类的schema,模型就会严格按照这个schema返回信息。

此外,我还发现一个好像很多模型都会忽视的一个问题,就是当提供的上下文,如{text}的内容过长时,许多模型的tool_calls就会失败。比如当提供了4K内容给{text}的时候,许多模型就开始忽略tool_calls的调用,转为普通的输出了。因此测试的时候,可以考虑增加指令、上下文长度和tool_calls的成功率的测试。

lobehubbot commented 3 hours ago

👀 @twang2218

Thank you for raising an issue. We will investigate into the matter and get back to you as soon as possible. Please make sure you have given us as much context as possible.\ 非常感谢您提交 issue。我们会尽快调查此事,并尽快回复您。 请确保您已经提供了尽可能多的背景信息。