Temperature控制输出的准确性和创造性
This commit is contained in:
parent
147eb780f4
commit
fd4f7b3ba7
|
|
@ -0,0 +1,54 @@
|
||||||
|
from langchain_core.messages import HumanMessage, SystemMessage
|
||||||
|
from langchain_core.output_parsers import StrOutputParser
|
||||||
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
|
from langchain_ollama.chat_models import ChatOllama
|
||||||
|
|
||||||
|
llm = ChatOllama(
|
||||||
|
model="qwen2.5:7b"
|
||||||
|
)
|
||||||
|
|
||||||
|
# ==================== 方式1: 直接使用 llm.stream() ====================
|
||||||
|
print("=" * 50)
|
||||||
|
print("方式1: 直接使用 llm.stream()")
|
||||||
|
print("=" * 50)
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
SystemMessage(content="你是一个Python专家,回答要简洁,不超过50字"),
|
||||||
|
HumanMessage(content="什么是列表推导式?")
|
||||||
|
]
|
||||||
|
|
||||||
|
print("回答: ", end="", flush=True)
|
||||||
|
for chunk in llm.stream(messages):
|
||||||
|
print(chunk.content, end="", flush=True)
|
||||||
|
print("\n")
|
||||||
|
|
||||||
|
# ==================== 方式2: 在链中使用 stream() ====================
|
||||||
|
print("=" * 50)
|
||||||
|
print("方式2: 在链中使用 chain.stream()")
|
||||||
|
print("=" * 50)
|
||||||
|
|
||||||
|
# 创建链:prompt | llm | parser
|
||||||
|
prompt = ChatPromptTemplate.from_template("用一句话解释{concept}")
|
||||||
|
parser = StrOutputParser()
|
||||||
|
chain = prompt | llm | parser
|
||||||
|
|
||||||
|
# 使用 stream() 方法进行流式输出
|
||||||
|
print("回答: ", end="", flush=True)
|
||||||
|
for chunk in chain.stream({"concept": "列表推导式"}):
|
||||||
|
print(chunk, end="", flush=True)
|
||||||
|
print("\n")
|
||||||
|
|
||||||
|
# ==================== 方式3: 链中不使用 parser,直接流式输出 LLM ====================
|
||||||
|
print("=" * 50)
|
||||||
|
print("方式3: 链中不使用 parser,直接流式输出 LLM 的 content")
|
||||||
|
print("=" * 50)
|
||||||
|
|
||||||
|
# 不使用 parser,直接输出 LLM 的 AIMessage
|
||||||
|
chain_without_parser = prompt | llm
|
||||||
|
|
||||||
|
print("回答: ", end="", flush=True)
|
||||||
|
for chunk in chain_without_parser.stream({"concept": "装饰器"}):
|
||||||
|
# chunk 是 AIMessageChunk,需要访问 content 属性
|
||||||
|
if hasattr(chunk, 'content'):
|
||||||
|
print(chunk.content, end="", flush=True)
|
||||||
|
print("\n")
|
||||||
|
|
@ -0,0 +1,129 @@
|
||||||
|
"""
|
||||||
|
Temperature 参数详解
|
||||||
|
==================
|
||||||
|
|
||||||
|
Temperature 是控制 LLM 输出随机性的重要参数。
|
||||||
|
|
||||||
|
作用原理:
|
||||||
|
- Temperature 值越高,模型输出的随机性越大,创造性越强
|
||||||
|
- Temperature 值越低,模型输出越确定,更倾向于选择概率最高的词
|
||||||
|
|
||||||
|
取值范围:
|
||||||
|
- 通常范围:0.0 - 2.0
|
||||||
|
- 推荐范围:0.0 - 1.0
|
||||||
|
- 默认值:通常为 0.7 或 1.0(取决于模型)
|
||||||
|
|
||||||
|
使用场景:
|
||||||
|
- temperature=0: 需要确定性答案(如代码生成、翻译、问答)
|
||||||
|
- temperature=0.3-0.7: 平衡创造性和准确性(如内容创作、对话)
|
||||||
|
- temperature=0.8-1.5: 需要创造性(如创意写作、头脑风暴)
|
||||||
|
"""
|
||||||
|
|
||||||
|
from langchain_ollama import ChatOllama
|
||||||
|
|
||||||
|
question = "用一句话解释什么是Python"
|
||||||
|
|
||||||
|
print("=" * 70)
|
||||||
|
print("Temperature 参数对比演示")
|
||||||
|
print("=" * 70)
|
||||||
|
print(f"\n问题: {question}\n")
|
||||||
|
|
||||||
|
# ==================== Temperature = 0 (完全确定性) ====================
|
||||||
|
print("-" * 70)
|
||||||
|
print("Temperature = 0 (完全确定性,每次输出相同)")
|
||||||
|
print("-" * 70)
|
||||||
|
|
||||||
|
llm_temp0 = ChatOllama(model="qwen2.5:7b", temperature=0)
|
||||||
|
|
||||||
|
print("第1次调用:")
|
||||||
|
response1 = llm_temp0.invoke(question)
|
||||||
|
print(f" {response1.content}\n")
|
||||||
|
|
||||||
|
print("第2次调用:")
|
||||||
|
response2 = llm_temp0.invoke(question)
|
||||||
|
print(f" {response2.content}\n")
|
||||||
|
|
||||||
|
print("第3次调用:")
|
||||||
|
response3 = llm_temp0.invoke(question)
|
||||||
|
print(f" {response3.content}\n")
|
||||||
|
|
||||||
|
# ==================== Temperature = 0.7 (平衡) ====================
|
||||||
|
print("-" * 70)
|
||||||
|
print("Temperature = 0.7 (平衡创造性和准确性)")
|
||||||
|
print("-" * 70)
|
||||||
|
|
||||||
|
llm_temp07 = ChatOllama(model="qwen2.5:7b", temperature=0.7)
|
||||||
|
|
||||||
|
print("第1次调用:")
|
||||||
|
response1 = llm_temp07.invoke(question)
|
||||||
|
print(f" {response1.content}\n")
|
||||||
|
|
||||||
|
print("第2次调用:")
|
||||||
|
response2 = llm_temp07.invoke(question)
|
||||||
|
print(f" {response2.content}\n")
|
||||||
|
|
||||||
|
print("第3次调用:")
|
||||||
|
response3 = llm_temp07.invoke(question)
|
||||||
|
print(f" {response3.content}\n")
|
||||||
|
|
||||||
|
# ==================== Temperature = 1.0 (高随机性) ====================
|
||||||
|
print("-" * 70)
|
||||||
|
print("Temperature = 1.0 (高随机性,每次输出可能不同)")
|
||||||
|
print("-" * 70)
|
||||||
|
|
||||||
|
llm_temp1 = ChatOllama(model="qwen2.5:7b", temperature=1.0)
|
||||||
|
|
||||||
|
print("第1次调用:")
|
||||||
|
response1 = llm_temp1.invoke(question)
|
||||||
|
print(f" {response1.content}\n")
|
||||||
|
|
||||||
|
print("第2次调用:")
|
||||||
|
response2 = llm_temp1.invoke(question)
|
||||||
|
print(f" {response2.content}\n")
|
||||||
|
|
||||||
|
print("第3次调用:")
|
||||||
|
response3 = llm_temp1.invoke(question)
|
||||||
|
print(f" {response3.content}\n")
|
||||||
|
|
||||||
|
# ==================== 不同场景的推荐值 ====================
|
||||||
|
print("=" * 70)
|
||||||
|
print("不同场景的 Temperature 推荐值")
|
||||||
|
print("=" * 70)
|
||||||
|
|
||||||
|
scenarios = [
|
||||||
|
("代码生成", 0.0, "需要准确、可执行的代码"),
|
||||||
|
("翻译任务", 0.0, "需要准确、一致的翻译"),
|
||||||
|
("问答系统", 0.0, "需要准确、事实性的答案"),
|
||||||
|
("内容总结", 0.3, "需要准确但略有变化的总结"),
|
||||||
|
("对话助手", 0.7, "需要自然、多样化的回复"),
|
||||||
|
("创意写作", 0.9, "需要创造性、多样化的表达"),
|
||||||
|
("头脑风暴", 1.0, "需要最大化的创意和多样性"),
|
||||||
|
]
|
||||||
|
|
||||||
|
print("\n场景\t\t\tTemperature\t说明")
|
||||||
|
print("-" * 70)
|
||||||
|
for scenario, temp, desc in scenarios:
|
||||||
|
print(f"{scenario:15}\t{temp}\t\t{desc}")
|
||||||
|
|
||||||
|
print("\n" + "=" * 70)
|
||||||
|
print("总结")
|
||||||
|
print("=" * 70)
|
||||||
|
print("""
|
||||||
|
1. Temperature = 0:
|
||||||
|
- 优点:输出稳定、可预测、适合需要准确性的任务
|
||||||
|
- 缺点:缺乏创造性、可能显得机械化
|
||||||
|
|
||||||
|
2. Temperature = 0.3-0.7:
|
||||||
|
- 优点:平衡准确性和创造性
|
||||||
|
- 适用:大多数通用场景
|
||||||
|
|
||||||
|
3. Temperature = 0.8-1.5:
|
||||||
|
- 优点:输出多样化、有创造性
|
||||||
|
- 缺点:可能不够准确、输出不稳定
|
||||||
|
- 适用:创意任务、需要多样性的场景
|
||||||
|
|
||||||
|
提示:
|
||||||
|
- 对于相同输入,temperature=0 时多次调用结果相同
|
||||||
|
- temperature>0 时,每次调用结果可能不同
|
||||||
|
- 根据任务类型选择合适的 temperature 值
|
||||||
|
""")
|
||||||
Loading…
Reference in New Issue