在复杂的查询分析中,语言模型可能难以理解其在某些场景中的具体响应方式。为改进此类场景下的性能,我们可以在提示中添加示例来引导LLM(语言模型)。接下来,我将展示如何为我们在快速入门中构建的LangChain YouTube视频查询分析器添加示例。
技术背景介绍
为了创建一个能够处理复杂查询的智能分析器,我们通常需要引入一种机制,让模型理解如何根据上下文或问题格式提供响应。通过在提示中加入实例以及与之对应的理想输出,模型可以更好地解构和回答用户的问题。
核心原理解析
引入实例的核心原理是通过提供明确的输入与输出配对示例,指导模型了解如何处理类似的未见过的问题。实例作为指引性的模板,帮助模型在遇到相似问题时,能更好地拆解和组合生成答案。
代码实现演示
以下步骤说明如何为LangChain视频查询分析器添加示例。
设置环境
# 安装必要的库
%pip install -qU langchain-core langchain-openai
配置API密钥
我们将使用OpenAI服务:
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass() # 输入您的API密钥
定义查询模式
from typing import List, Optional
from langchain_core.pydantic_v1 import BaseModel, Field
class Search(BaseModel):
query: str = Field(
...,
description="Primary similarity search query applied to video transcripts.",
)
sub_queries: List[str] = Field(
default_factory=list,
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. \
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
query_analyzer.invoke(
"what's the difference between web voyager and reflection agents? do both use langgraph?"
)
添加示例和优化提示
通过添加实例,我们可以进一步细化查询。以下是如何添加示例:
examples = [
{
"input": "What's chat langchain, is it a langchain template?",
"tool_calls": [
Search(
query="What is chat langchain and is it a langchain template?",
sub_queries=["What is chat langchain", "What is a langchain template"],
)
],
},
...
]
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
SystemMessage,
ToolMessage,
)
import uuid
def tool_example_to_messages(example):
messages = [HumanMessage(content=example["input"])]
openai_tool_calls = [
{
"id": str(uuid.uuid4()),
"type": "function",
"function": {
"name": tool_call.__class__.__name__,
"arguments": tool_call.json(),
},
}
for tool_call in example["tool_calls"]
]
messages.append(AIMessage(content="", additional_kwargs={"tool_calls": openai_tool_calls}))
return messages
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?"
)
应用场景分析
- 视频教程查询分析:应用于从大量视频教程中自动提取相关信息。
- 复杂问题分解:在技术支持、客户服务等需要多层次解答的场景下尤为适用。
- 智能问答系统:集成到商业智能平台,提升自动问答的精准性。
实践建议
- 示例选择:选择能够代表各种问题复杂度的示例。
- 逐步实验:开始时可用简单示例,随着需求增加逐步复杂化。
- 反复测试:通过不断修改和测试,优化示例的质量和数量。
如果遇到问题欢迎在评论区交流。
—END—

被折叠的 条评论
为什么被折叠?



