美化输出
代码实现
WikipediaRetriever
维基百科API检索器。
它将load()包装为get_relevant_documents()。它使用所有WikipediaAPIWrapper参数,没有任何更改。
通过分析和验证来自关键字参数的输入数据来创建一个新模型。
如果无法解析输入数据以形成有效的模型,则引发ValidationError。
#下载依赖
from langchain_community.retrievers import WikipediaRetriever
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.llms import Ollama
from langchain_experimental.llms.ollama_functions import OllamaFunctions
#模型实例化
llm = Ollama(model='llama2')
#下载维基百科信息
wiki = WikipediaRetriever(top_k_results=6, doc_content_chars_max=2000)
#提示词模版
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You're a helpful AI assistant. Given a user question and some Wikipedia article snippets, answer the user question. If none of the articles answer the question, just say you don't know.\n\nHere are the Wikipedia articles:{context}",
),
("human", "{question}"),
]
)
prompt.pretty_print()#美化输出
结果返回
现在我们已经有了一个模型、检索器和提示,让将它们链接在一起。需要添加一些逻辑,将检索到的文档格式化为可以传递给提示的字符串。以便链的同时返回答案和检索到的文档。
依赖加载
from operator import itemgetter
from typing import List
from langchain_core.documents import Document
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import (
RunnableLambda,
RunnableParallel,
RunnablePassthrough,
)
格式转换
def format_docs(docs: List[Document]) -> str:
"""将文档转换为单个字符串.:"""
formatted = [
f"Article Title: {
doc.metadata['title']}\nArticle Snippet: {
doc.page_content}"
for doc in docs
]
return "\n\n" + "\n\n".join(formatted)
输出
format = itemgetter("docs") | RunnableLambda(format_docs)
# 检索完成后生成答案的子通道
answer = prompt | llm | StrOutputParser()
# 调用wiki->将文档格式化为字符串的完整链->运行answer子链->返回答案和检索到的文档。
chain = (
RunnableParallel(question=RunnablePassthrough(), docs=wiki) #格式化
.assign(context=format)#输出样式
.assign(answer=answer)
.pick(["answer", "docs"])
)
print(chain.invoke("How fast are cheetahs?"))
返回:
function-calling
在编程中,“function-calling”(函数调用)是一个基本的概念,指的是从程序的某一部分调用或激活一个函数(function)的行为。函数是一段封装了特定逻辑的代码块,加载在大模型中,使得大模型能有特定的输出。
代码实现
from langchain_experimental.llms.ollama_functions import OllamaFunctions
#实例化模型
model = OllamaFunctions(model="mistral")
工具设计
llm_with_tool = model.bind(
#函数
functions=[
{
"name": "cited_answer",
"description": "Answer user questions based solely on the given source and cite the source used",#描述 仅根据给定的来源回答用户问题,并引用所使用的来源
"parameters": {
#参数
"type": "object",#类型 object
"properties": {
#特性 这里的内容是自定义的,下面主要是希望能让大模型给出关于人身高的相关信息。
'human':{
"type": "string",
"description": "Remember the names of people that have appeared before",
},
'characteristic':{
"type": "string",
"description": "Remember everyone's characteristics",
},
'return':{
"type": "string",
"description": "Answer first questions based on the information obtained",
},
},
},
}
],
function_call={
"name": "cited_answer"},
)
加载提示词
example_q = """What Brian's height?
Source: 1
Information: Suzy is 6'2"
Source: 2
Information: Jeremiah is blonde
Source: 3
Information: Brian is 3 inches shorted than Suzy"""
#上面可以是希望的任何信息。
return_ = llm_with_tool.invoke(example_q)
return_
返回
AIMessage(content=‘’, additional_kwargs={‘function_call’: {‘name’:
‘cited_answer’, ‘arguments’: ‘{“human”: “Brian, Suzy”, “return”:
“Brian is 5 inches shorter than Susie, so his height is approximately 5 feet 8 inches (66 inches).”}’}},
id=‘run-c76f129f-06a0-44e9-95a6-a3ffd1760ffb-0’)
#查看详细信息
import json
return_1= return_.to_json()['kwargs']['additional_kwargs']['function_call']['arguments']
# 使用 json.loads 方法将字符串转换为字典
dict_data = json.loads(return_1)
dict_data['return']
Brian is 5 inches shorter than Susie, so his height is approximately 5 feet 8 inches (66 inches)
#布赖恩比苏西矮5英寸,所以他的身高大约是5英尺8英寸(66英寸)。这里看着回复可能比较合理
对比 没有使用function-calling的回答
提示词加载
#实例化模型
llm = Ollama(model='mistral')
#提示词模版
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
""""Source: 1
Information: Suzy is 6'2"
Source: 2
Information: Jeremiah is blonde
Source: 3
Information: Brian is 3 inches shorted than Suzy""",
),
("human", "{question}"),
]
)
输出
chain = prompt | llm
return_=chain.invoke("What Brian's height?")
return_
" Based on the information provided, Suzy is 6 feet 2 inches tall, and
Brian is 3 inches shorter than her. To find Brian’s height, we
subtract 3 inches from Suzy’s height:\n\n6 feet 2 inches (Suzy’s
height) - 3 inches (height difference) = 5 feet 11 inches (Brian’s
height)\n\nTherefore, Brian is 5 feet 11 inches tall."
从这里看回答的会有点繁琐。
以上是本文的全部内容,感谢阅读。
·