趋近智
构建一个简单的问答机器人,从而展示实际应用。本次练习旨在培养调取API、发送提示词 (prompt)及以程序方式处理回复的能力。将创建一个命令行程序,用于接收用户提问,通过LLM的API发送给LLM,并显示回答。
开始前,请确保已进行以下设置:
openai 库。通过pip安装:
pip install openai
OPENAI_API_KEY 的环境变量,并填入你的实际密钥。export OPENAI_API_KEY='your_api_key_here'set OPENAI_API_KEY=your_api_key_here$env:OPENAI_API_KEY='your_api_key_here'我们的机器人将循环执行以下步骤:
temperature 或 max_tokens)。我们来编写Python代码。创建一个名为 qa_bot.py 的文件。
import os
import openai
from openai import OpenAI # Use the updated OpenAI library structure
# --- 配置 ---
# 尝试从环境变量加载API密钥
try:
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
except openai.OpenAIError as e:
print(f"Error initializing OpenAI client: {e}")
print("Please ensure the OPENAI_API_KEY environment variable is set.")
exit()
MODEL_NAME = "gpt-3.5-turbo" # 或选择其他合适的模型
# --- 核心功能 ---
def get_llm_response(user_question):
"""
将用户的问题发送到LLM API并返回回复。
"""
system_prompt = "You are a helpful assistant. Answer the user's question clearly and concisely."
try:
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_question}
],
temperature=0.7, # 调整以平衡创造性和确定性
max_tokens=150 # 限制回复长度
)
# 提取回复文本
# 注意:确切的结构可能因API版本更新而略有不同
if completion.choices and len(completion.choices) > 0:
response_text = completion.choices[0].message.content.strip()
return response_text
else:
return "Error: No response received from the API."
except openai.APIConnectionError as e:
return f"API Connection Error: {e}"
except openai.RateLimitError as e:
return f"API Rate Limit Exceeded: {e}"
except openai.AuthenticationError as e:
return f"API Authentication Error: {e}. Check your API key."
except openai.APIError as e:
return f"API Error: {e}"
except Exception as e:
# 捕获任何其他意外错误
return f"An unexpected error occurred: {e}"
# --- 主交互循环 ---
def main():
print("Simple Q&A Bot (type 'quit' or 'exit' to stop)")
print("-" * 30)
while True:
user_input = input("You: ")
if user_input.lower() in ["quit", "exit"]:
print("Bot: Goodbye!")
break
if not user_input:
continue
print("Bot: Thinking...") # 等待时提供反馈
llm_answer = get_llm_response(user_input)
print(f"Bot: {llm_answer}")
print("-" * 30)
if __name__ == "__main__":
main()
os 用于访问环境变量,以及 openai 用于API交互。OpenAI 客户端。API密钥从 OPENAI_API_KEY 环境变量中获取。此处包含基本的错误处理,以捕获初始化问题。我们还定义了 MODEL_NAME。get_llm_response(user_question) 函数:
user_question 作为输入。system_prompt,为LLM提供上下文 (context)或指令(在此处是设置其角色)。client.chat.completions.create 调用API。这是用于像GPT-3.5 Turbo或GPT-4这样的聊天型模型的标准方法。messages 参数 (parameter)遵循聊天格式,包含角色(“system”、“user”)。temperature 和 max_tokens 作为本章前面讨论的参数示例被包含在内。temperature=0.7 允许回复具有一定的变化性,而 max_tokens=150 则限制长度以避免回答过长。completion.choices[0].message.content 获取回复内容。此处回复对象的结构很重要;如果你使用不同的库或模型,在开发过程中可能需要查看实际的回复对象(print(completion))。try...except 代码块捕获特定的 openai 异常(如连接错误、速率限制、认证问题)以及通用异常,并返回有用的错误信息,而不是导致机器人崩溃。main() 函数:
while True 循环以进行持续交互。input("You: ") 提示用户并读取其输入。get_llm_response。if __name__ == "__main__"::这个标准的Python结构可确保 main() 函数仅在脚本直接执行时才被调用。qa_bot.py。OPENAI_API_KEY 环境变量在终端会话中已设置。python qa_bot.py
简单问答机器人(输入“quit”或“exit”停止)
------------------------------
你:法国的首都在哪里?
机器人:思考中...
机器人:法国的首都是巴黎。
------------------------------
你:解释一下LLM API中温度(temperature)的含义。
机器人:思考中...
机器人:在LLM API中,“温度”(temperature)参数控制输出的随机性。较低的温度(例如0.2)会使输出更集中、更确定,通常选择最有可能的下一个词。较高的温度(例如0.8)会增加随机性,导致更具创造性或多样化的回复,但也可能不够连贯。它本质上调整了下一个token预测的概率分布。
------------------------------
你:quit
机器人:再见!
这个机器人功能基本,但它呈现了核心的交互模式。以下是一些你可以扩展它的方法:
get_llm_response 函数,使其接受并将之前的对话轮次包含在 messages 列表中。这使得LLM能够了解之前讨论过的内容。(在后续关于框架的章节中有所介绍)。temperature、max_tokens、top_p 等,以观察它们如何影响不同类型问题的输出质量和风格。这个动手练习提供了一个实际范例,展示了本章介绍的API交互思路如何转化为一个可运行的应用。成功搭建并运行这个机器人,标志着你在将LLM融入自己的软件项目中迈出了重要一步。
简洁的语法。内置调试功能。从第一天起就可投入生产。
为 ApX 背后的 AI 系统而构建
这部分内容有帮助吗?
© 2026 ApX Machine LearningAI伦理与透明度•