DeepSeek中的自学应用参数配置理解:
model="deepseek-flash",
选哪个模型。 这里用的是 DeepSeek 的 Flash 版,特点是快、便宜,适合日常对话、简单任务。它默认是思考模式,模型会先"想"再答,思考过程也会占 token 计费。如果你不需要思考、只想快速出结果,可以通过切换 Chat 模型不会思考,直接输出,自学自研更省钱。
messages=[
{"role": "system", "content": "写一句关于夏天奶茶的宣传语,15-20个字以内,不要加引号"},
],
给模型下指令。 system 角色相当于"设定规则的人",告诉模型你要它干什么。这里就是让它写一句奶茶宣传语,字数控制在15-20字,别加引号。
temperature=1,
控制模型"放飞"的程度。 值越大越有创意、越随机;值越小越保守、越稳定。1 是默认值,属于正常发挥,不太死板也不太飘。
top_p=0.5,
控制模型选词的范围大小。 可以理解为"候选词池子"的大小:0.5 表示只从概率最高的前50%候选词里挑,输出会比较集中、变化少;1.0 则放开所有限制,输出更多样。一般默认 0.9 左右,你这里设 0.5 会让输出偏保守。
💡 temperature 和 top_p 都影响随机性,通常只调其中一个就好,两个同时压低会让输出非常死板。
max_tokens=20,
限制模型最多输出多少个 token。 这里设了 20,对应你的"15-20个字以内"需求。token 不完全等于汉字(中文大约1-2字=1 token),所以这个值要留点余量。
stop=["语言"],
停止序列。 告诉模型:一旦你生成的内容里出现了 "语言" 这两个字,立刻停笔,后面的别写了。适合用来强制截断不想要的内容。
stream=True,
流式输出。 设为 True 后,模型会像打字机一样逐字往外吐,用户能实时看到生成过程,体验更好;设为 False 则要等全部生成完才一次性返回。
一句话总结
这段配置就是:用 Flash 模型,让它写一句奶茶宣传语,输出控制在20个token以内,遇到"语言"就停,打字机式实时返回,创意程度中等偏保守。
代码完整实例:
# Please install OpenAI SDK first: `pip3 install openai`
# dotenv
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(
api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url=os.environ.get('DEEPSEEK_BASE_URL'),
)
def slogan():
response = client.chat.completions.create(
# 如果写flash 默认思考模式,思考占用了token长度 chat不是思考模式
model="deepseek-flash",
messages=[
{"role": "system", "content": "写一句关于夏天奶茶的宣传语,15-20个字以内,不要加引号"},
],
#温度参数 越小越聚合 越大越分散多样性 一般默认1
temperature=1,
# 模型选词的范围 核采样 (0-1]范围 值越小输出变化雷同,1.0输出多样
top_p=0.5,
# 限制输出的长度
max_tokens=20,
# stop=["\n"] 停止序列
stop=["语言"],
# 流式输出
stream=True
)
return response.choices[0].message.content.strip()
本文由 AI图鉴自动抓取并翻译,版权归原作者所有;如需查看原文请点下方链接。
The self-learning application parameter configuration in DeepSeek understands:
model="deepseek-flash",
Choose which model. It's a Flash version of DeepSeek, which is fast and cheap, suitable for daily conversations and simple tasks. It's by default a mode of thinking, a model that "thinks" before answering, and the process of thinking is token. If you don't need to think, you just want to produce quick results, you can switch to the Chat model, you can't think, you can export, you can learn to save money.
messages=[
♪ "Role": "system", "content" ♪ Write a message for summer tea, 15-20 words, no quotes ♪
],
Command the model. The role of system is equivalent to "the rule-maker," telling the model what you want it to do. This is where it's supposed to write a milk and tea pamphlet. The word count is 15-20 words.
temperature=1,
Control the extent of the model "dejection". The larger the value, the more creative and random; the smaller the value, the more conservative and stable. 1 Default, normal and not rigid.
top_p=0.5,
Controls the range size of the model selected word. It can be understood as the size of the "selection pool": 0.5 means that the top 50% of the most probabilities are chosen, and the output is more focused and less variable; 1.0 releases all the limits and more outputs. General default of 0.9, you set 0.5 here to make the output conservative.
typerature and top p influence randomity, usually just one of them, and both at the same time press down make the output very rigid.
max_tokens=20,
Limit how many tokens the model can output. Here's 20 for your "15-20" needs. Token is not exactly equivalent to Han (approximately 1-2 words in Chinese = 1 token), so this value has to be left behind.
Stop = ["Language"],
Stop the sequence. Tell the model: once the word "language" appears in the content you generate, stop the pen and leave it behind. It's appropriate to force the cut-off of unwanted content.
stream=True,
Stream output. When set to True, the model will be thrown out word by word, like a typewriter, and the user will be able to see the process in real time and experience it better; setting to False will only return once all is produced.
Summary
The configuration is: using the Flash model to write a milk and tea pamphlet, output is contained in 20 tokens, stop when you encounter a "linguistic" and typewriter returns in real time, with a moderate level of creativity.
Complete examples of codes:
# Please install OpenAI SDK first: `pip3 install openai`
# dotenv
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(
api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url=os.environ.get('DEEPSEEK_BASE_URL'),
)
def slogan():
response = client.chat.completions.create(
# Thinking takes up token length and chat is not thinking
model="deepseek-flash",
messages=[
♪ "Role": "system", "content" ♪ Write a message for summer tea, 15-20 words, no quotes ♪
],
# The temperature parameters, the smaller they are, the more aggregate they are, the bigger they are, the more diverse they are, the default one.
temperature=1,
# The range of model words, the range of nuclear sampling (0-1], the smaller the value of output changes the mine, and the output varies by 1.0
top_p=0.5,
# Limit the length of the output
max_tokens=20,
Stop=["n"] Stop the sequence
Stop = ["Language"],
# Fluid output
stream=True
)
return response.choices[0].message.content.strip()
Auto-fetched and translated by AI Atlas. All rights belong to the original author.