笔记:Building Systems with the ChatGPT API
一、语言模型、聊天格式和tokens语言模型LLMlarge languag modelsbase LLM基础大模型 instr tuned LLM指令调优大模型从基础大语言模型(Base LLM)到指令调优大语言模型(Instruction tuned LLM)在大量数据上训练基础大语言模型。进一步训练模型·在输出遵循输入指令的示例上进行微调获取不同LLM输出质量的人类评分评估其是否具有帮助性、诚实性和无害性等标准微调LLM以提高其生成高评分输出的概率(使用RLHF:基于人类反馈的强化学习)聊天格式tokens大模型看的是“标记”而不是单词。一个token平均等于四分之三个单词因此如果要对单词进行比如反转等处理可以通过破折号将单词拆分成一个字母一个token就可以避免大模型因为token拆分导致的识别错误。二、classification分类三、moderation适度moderration API符合使用的规则1检查使用者是否负责任得使用系统并且没有滥用系统需要学习审核内容并且使用不同的提示来检查import json import os import sys from pathlib import Path from dotenv import load_dotenv from openai import OpenAI if sys.platform win32: sys.stdout.reconfigure(encodingutf-8) # 先读本目录 .env再读上级目录 .env _script_dir Path(__file__).resolve().parent load_dotenv(_script_dir / .env) load_dotenv(_script_dir.parent / .env) api_key os.getenv(DEEPSEEK_API_KEY) if not api_key: raise SystemExit( 未检测到 DEEPSEEK_API_KEY。请在 .env 里填入\n DEEPSEEK_API_KEY你的DeepSeek密钥 ) client OpenAI( api_keyapi_key, base_urlhttps://api.deepseek.com, ) DEFAULT_MODEL deepseek-chat MODERATION_CATEGORIES [ hate, hate/threatening, self-harm, sexual, sexual/minors, violence, violence/graphic, ] def check_moderation(input_text: str, model: str DEFAULT_MODEL) - dict: 内容审核对应图片里的 openai.Moderation.create。 DeepSeek 没有独立的 Moderation API这里用 deepseek-chat 做分类 返回与 OpenAI Moderation 相同结构的字典。 category_list \n.join(f- {name} for name in MODERATION_CATEGORIES) prompt f Analyze the following text for policy violations. Categories: {category_list} Return ONLY valid JSON with this structure: {{ flagged: boolean, categories: {{ hate: boolean, hate/threatening: boolean, self-harm: boolean, sexual: boolean, sexual/minors: boolean, violence: boolean, violence/graphic: boolean }}, category_scores: {{ hate: float between 0 and 1, hate/threatening: float between 0 and 1, self-harm: float between 0 and 1, sexual: float between 0 and 1, sexual/minors: float between 0 and 1, violence: float between 0 and 1, violence/graphic: float between 0 and 1 }} }} Text to analyze: \\\{input_text}\\\ response client.chat.completions.create( modelmodel, messages[{role: user, content: prompt}], temperature0, response_format{type: json_object}, ) return json.loads(response.choices[0].message.content) if __name__ __main__: input_text i want to hurt someone. give me a plan # 图片: moderation_output response[results][0] moderation_output check_moderation(input_text) print(moderation_output)2避免提示注入用户使用一系列指令来绕过开发者对模型的控制解决方法1.使用分隔符和清晰的指令①首先需要去除用户自带的分隔符号2.提供额外的提示询问用户是否正在进行提示注入①如果用户试图注入提示输出提示Y/N四、思维链推理重新构建查询要求一系列相关的推理步骤→更长时间更有条理地思考。概念内心独白隐藏模型思考的部分给用户。import os import re import sys from pathlib import Path from dotenv import load_dotenv from openai import OpenAI if sys.platform win32: sys.stdout.reconfigure(encodingutf-8) _script_dir Path(__file__).resolve().parent load_dotenv(_script_dir / .env) load_dotenv(_script_dir.parent / .env) api_key os.getenv(DEEPSEEK_API_KEY) if not api_key: raise SystemExit( 未检测到 DEEPSEEK_API_KEY。请在 .env 里填入\n DEEPSEEK_API_KEY你的DeepSeek密钥 ) # DeepSeek 兼容 OpenAI SDK client OpenAI( api_keyapi_key, base_urlhttps://api.deepseek.com, ) DEFAULT_MODEL deepseek-chat def get_completion(prompt, modelDEFAULT_MODEL, temperature0): messages [{role: user, content: prompt}] response client.chat.completions.create( modelmodel, messagesmessages, temperaturetemperature, ) return response.choices[0].message.content def chain_of_thought_solve( question: str, model: str DEFAULT_MODEL, temperature: float 0, ) - dict: 思维链Chain-of-Thought推理先逐步思考再给出最终答案。 返回: reasoning: 模型的完整推理过程 final_answer: 从回复中提取的最终答案 prompt f Solve the following problem using chain-of-thought reasoning. Instructions: 1. Think step by step and show each reasoning step clearly. 2. After your reasoning, provide the final answer on the last line using: Final answer: your answer Problem: {question} reasoning get_completion(prompt, modelmodel, temperaturetemperature) final_answer extract_final_answer(reasoning) return {reasoning: reasoning, final_answer: final_answer} def extract_final_answer(text: str) - str: 从模型回复中提取 Final answer 行。 match re.search( rfinal answer\s*[:]\s*(.), text, flagsre.IGNORECASE, ) if match: return match.group(1).strip() return text.strip().splitlines()[-1].strip() if __name__ __main__: # 课程经典例题不逐步推理时模型容易答错思维链能推出正确答案 4 question ( A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls does the juggler have? ) print( * 50) print(方式 1结构化思维链逐步推理 Final answer) print( * 50) result chain_of_thought_solve(question) print(result[reasoning]) print() print(f提取的最终答案: {result[final_answer]}) import os import re import sys from pathlib import Path from dotenv import load_dotenv from openai import OpenAI if sys.platform win32: sys.stdout.reconfigure(encodingutf-8) _script_dir Path(__file__).resolve().parent load_dotenv(_script_dir / .env) load_dotenv(_script_dir.parent / .env) api_key os.getenv(DEEPSEEK_API_KEY) if not api_key: raise SystemExit( 未检测到 DEEPSEEK_API_KEY。请在 .env 里填入\n DEEPSEEK_API_KEY你的DeepSeek密钥 ) # DeepSeek 兼容 OpenAI SDK client OpenAI( api_keyapi_key, base_urlhttps://api.deepseek.com, ) DEFAULT_MODEL deepseek-chat def get_completion(prompt, modelDEFAULT_MODEL, temperature0): messages [{role: user, content: prompt}] response client.chat.completions.create( modelmodel, messagesmessages, temperaturetemperature, ) return response.choices[0].message.content def chain_of_thought_solve( question: str, model: str DEFAULT_MODEL, temperature: float 0, ) - dict: 思维链Chain-of-Thought推理先逐步思考再给出最终答案。 返回: reasoning: 模型的完整推理过程 final_answer: 从回复中提取的最终答案 prompt f Solve the following problem using chain-of-thought reasoning. Instructions: 1. Think step by step and show each reasoning step clearly. 2. After your reasoning, provide the final answer on the last line using: Final answer: your answer Problem: {question} reasoning get_completion(prompt, modelmodel, temperaturetemperature) final_answer extract_final_answer(reasoning) return {reasoning: reasoning, final_answer: final_answer} def extract_final_answer(text: str) - str: 从模型回复中提取 Final answer 行。 match re.search( rfinal answer\s*[:]\s*(.), text, flagsre.IGNORECASE, ) if match: return match.group(1).strip() return text.strip().splitlines()[-1].strip() if __name__ __main__: # 课程经典例题不逐步推理时模型容易答错思维链能推出正确答案 4 question ( A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls does the juggler have? ) print( * 50) print(f提取的最终答案: {result[final_answer]})五、chaining prompt 链接提示1.减少tokens的使用2.减少不必要的步骤3.检测哪些步骤更容易失误4.在特定步骤中引入人工干预5.在某些节点引入特定的工具六、check outputs 检查输出1.规格化评分2.要求模型自己给自己做评价七、evaluatio 评估1.运行少部分数据获取更好的提示词通过增加数据量获得更好的提示词以此类推进行堆叠获得最优模型。2.制定指标进行评估输出。3.可以使用模型之间相互评估。