文章目录
- 前言
- 大模型对话函数
- 返回列表格式
- 返回对象类型
- 返回对象格式(信息抽取)
- 返回json格式
- 做选择题(在给定答案中选一个答案)
前言
这里大模型使用GLM4。
结构化输出的稳定性与大模型的能力挂钩
听话的就容易出
不听话的就容易报错
langchain实现大模型结构化输出,实际上就帮助我们构建一套提示词
对模型的约束也是十分有限的
但是仍然可以用
大模型对话函数
import requests
import json
from typing import *
from openai import OpenAI
base_url = "http://127.0.0.1:8023/v1/"
client = OpenAI(api_key="EMPTY", base_url=base_url)
def simple_chat(use_stream=False, query=None):
messages = []
messages.append(
{
"role": "user",
"content": query
}
)
response = client.chat.completions.create(
model="glm-4",
messages=messages,
stream=use_stream,
max_tokens=256,
temperature=0.4,
presence_penalty=1.2,
top_p=0.8,
)
# 检查响应是否存在并且有 'choices' 键
if response and hasattr(response, 'choices'):
# 打印出所有的选择(回答)
for choice in response.choices:
# 检查 'message' 键是否存在于选择中
if hasattr(choice, 'message'):
# 打印回答的内容
print(choice.message.content)
return choice.message.content
else:
print("Error:", response.status_code if response else "No response received.")
首先GLM4使用openai的方式启动,然后配置好端口号就行了。
输入一句回答一句
代码参考GLM4官方的代码修改而来。
返回列表格式
# 返回列表格式
from langchain_core.output_parsers.list import CommaSeparatedListOutputParser
from langchain_core.prompts import PromptTemplate
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
template="列出几种{subject}.\n{format_instructions}",
input_variables=["subject"],
partial_variables={"format_instructions": format_instructions + "用中文回答"}
)
prompt = prompt.format(subject="水果")
print("prompt"+"*"*10)
print(prompt)
print("prompt"+"*"*10)
prompt = str(prompt)
output = simple_chat(query=prompt)
print("output"+"*"*10)
print(output)
print("output_parser"+"*"*10)
print(output_parser.parse(output))
print("output_parser"+"*"*10)
output_parser就是一个结构化输出的提示词模板
prompt就是使用langchain模板构建的提示词
提示词输入大模型即可生成结果
结果由output_parser解析,就能得到我们需要的结构化输出结果
上述代码运行结果为:
也就是说langchain的工具帮助我们进行结构化输出,就是帮助我们构建结构化提示词和对结果进行结构化解析。
所以说,这里最重要的就是提示词的构建。
下面的讲述都只放出提示词,因为大模型喂入那部分都是相同的。
返回对象类型
from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.output_parsers.pydantic import PydanticOutputParser
from langchain_core.prompts import PromptTemplate
class Student(BaseModel):
name: str = Field(description="学生的姓名")
age: str = Field(description="学生的年龄")
student_query = "告诉我一个学生的信息"
output_parser = PydanticOutputParser(pydantic_object=Student)
prompt = PromptTemplate(
template="回答下面问题.\n{format_instructions}\n{query}\n",
input_variables=["query"],
partial_variables={"format_instructions": output_parser.get_format_instructions()+"用中文回答"},
)
prompt = prompt.format_prompt(query=student_query)
返回对象格式(信息抽取)
from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.output_parsers.pydantic import PydanticOutputParser
from langchain_core.prompts import PromptTemplate
class Person(BaseModel):
name: Optional[str] = Field(default=None, description="The name of the person")
hair_color: Optional[str] = Field(
default=None, description="The color of the peron's hair if known"
)
height_in_meters: Optional[str] = Field(
default=None, description="Height measured in meters"
)
text = "Alan Smith is 1.65m tall.\n"
query = text
output_parser = PydanticOutputParser(pydantic_object=Person)
prompt = PromptTemplate(
template='''system:\nYou are an expert extraction algorithm.\nOnly extract relevant information from the text.\nIf you do not know the value of an attribute asked to extract\nreturn null for the attribute's value.\n{format_instructions}\nhuman:{query}\n''',
input_variables=["query"],
partial_variables={"format_instructions": output_parser.get_format_instructions()},
)
prompt = prompt.format_prompt(query=query)
返回json格式
# 返回json格式
# case1
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain_core.prompts import PromptTemplate
response_schemas = [
ResponseSchema(name="name", description="学生的姓名"),
ResponseSchema(name="age", description="学生的年龄")
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
template="回答下面问题.\n{format_instructions}\n{question}",
input_variables=["question"],
partial_variables={"format_instructions": format_instructions}
)
prompt = prompt.format_prompt(question="给我一个女孩的信息")
# case2
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain_core.prompts import PromptTemplate
response_schemas = [
ResponseSchema(name="answer", description="地名"),
ResponseSchema(
name="source",
description="答案的网址",
),
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
template="answer the users question as best as possible.\n{format_instructions}\n{question}",
input_variables=["question"],
partial_variables={"format_instructions": format_instructions},
)
prompt = prompt.format_prompt(question="华为公司的总部在哪里?")
# 据说弱点的模型用上面的方式来返回json效果会好点
# case3
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
# Define your desired data structure.
class Joke(BaseModel):
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")
# And a query intented to prompt a language model to populate the data structure.
query = "讲一个笑话"
# Set up a parser + inject instructions into the prompt template.
output_parser = JsonOutputParser(pydantic_object=Joke)
prompt = PromptTemplate(
template="Answer the user query.\n{format_instructions}\n{query}\n",
input_variables=["query"],
partial_variables={"format_instructions": output_parser.get_format_instructions()},
)
prompt = prompt.format_prompt(query=query)
做选择题(在给定答案中选一个答案)
from langchain.output_parsers.enum import EnumOutputParser
from enum import Enum
class Colors(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
BLACK = "black"
OTHER = "other"
output_parser = EnumOutputParser(enum=Colors)
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
prompt = PromptTemplate.from_template(
"""这个人的眼睛可能是什么颜色的?你只需要说出答案就行了
> Person: {person}
Instructions: {instructions}"""
).partial(instructions=output_parser.get_format_instructions())
prompt = prompt.format_prompt(person="在中国的小明有一双水汪汪的眼睛")