FastAPI + Ollama AI提示词角色参数设定详细教程与示例
引言
随着AI技术的迅速发展,本地部署大语言模型(LLM)的需求日益增长。Ollama是一个免费开源框架,可以让大模型轻松运行在本地电脑上,而FastAPI是一个现代、高效的Python Web框架。将二者结合,可以创建高性能的AI应用。本文将详细介绍如何在FastAPI应用中通过Ollama API设置和使用AI模型的角色参数(personas),包括系统提示词、用户提示词和多轮对话管理。
环境搭建
在开始实际编码之前,需要先安装必要的依赖:
bash
复制
pip install fastapi ollama httpx
确保Ollama服务已经启动并在本地运行。默认情况下,Ollama API监听在http://localhost:11434
端口。
FastAPI与Ollama的基本集成
创建基本的FastAPI应用
首先,创建一个简单的FastAPI应用,用于通过Ollama API调用本地大语言模型:
python
复制
from fastapi import FastAPI
import httpx
app = FastAPI()
@app.get("/query")
async def query_ollama(model: str, prompt: str):
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/generate",
json={
"model": model,
"prompt": prompt
}
)
return {"result": response.json()["response"]}
这个简单的API端点可以通过/query?model=model_name&prompt=prompt_text
路径调用,使用指定的模型和提示词生成响应。
设置AI提示词角色参数
系统提示词(System Prompt)
系统提示词是定义AI助手角色、行为和任务范围的核心指令。在Ollama中,可以通过API请求中的system
参数设置系统提示词。
以下是使用系统提示词的完整FastAPI示例:
python
复制
from fastapi import FastAPI
import httpx
app = FastAPI()
@app.post("/chat")
async def chat_endpoint(request: dict):
model = request.get("model", "deepseek-chat")
messages = request.get("messages", [])
system_prompt = request.get("system_prompt", "你是一个专业的AI助手")
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": model,
"system": system_prompt,
"messages": messages
}
)
return {"result": response.json()}
在这个示例中,我们通过system
参数设置了系统提示词,定义了AI助手的角色和行为。messages
参数用于管理多轮对话的历史记录。
多轮对话管理
Ollama的chat
API支持多轮对话,能够附加历史记录,适合用于连续对话场景。下面是使用多轮对话的示例:
python
复制
# 第一次调用
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": "deepseek-chat",
"system": "你是一个专业的AI助手",
"messages": [
{"role": "user", "content": "你好,请帮我回答一个问题"}
]
}
)
# 获取响应
first_response = response.json()["response"]
# 第二次调用,包含历史对话
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": "deepseek-chat",
"system": "你是一个专业的AI助手",
"messages": [
{"role": "user", "content": "你好,请帮我回答一个问题"},
{"role": "assistant", "content": first_response},
{"role": "user", "content": "请告诉我2024年的诺贝尔物理学奖获得者是谁"}
]
}
)
# 获取最终响应
final_response = response.json()["response"]
不同角色参数示例
以下是几种常见的AI角色参数设定示例:
1. 专业AI助手角色
python
复制
system_prompt = """
你是一个专业的AI助手,擅长使用互联网和工具来解决用户的问题。
你必须严格按照以下规则进行:
1. 先分析用户请求,确定是否需要使用工具
2. 如果需要使用工具,调用相应的工具
3. 如果不需要使用工具,直接回答用户的问题
4. 如果不确定如何回答,可以向用户请求更多信息
"""
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": "deepseek-chat",
"system": system_prompt,
"messages": [{"role": "user", "content": "请告诉我2024年的诺贝尔物理学奖获得者是谁"}]
}
)
2. 专业翻译员角色
python
复制
system_prompt = """
你是一个专业的翻译员,擅长将英文翻译成指定的语言。
你需要按照以下步骤工作:
1. 首先确认用户需要将英文翻译成哪种语言
2. 然后将英文翻译成指定的语言
3. 如果翻译过程中遇到困难,可以向用户请求帮助
4. 最终提供翻译结果和原始英文文本
"""
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": "deepseek-chat",
"system": system_prompt,
"messages": [{"role": "user", "content": "Please translate 'Hello, how are you?' into Chinese"}]
}
)
3. 专业程序员角色
python
复制
system_prompt = """
你是一个专业的程序员,擅长使用代码解决用户的问题。
你需要按照以下步骤工作:
1. 首先确认用户需要解决的问题
2. 然后提供解决该问题的代码
3. 如果代码中有错误,可以向用户解释并请求反馈
4. 最终提供完整的解决方案和解释
"""
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": "deepseek-chat",
"system": system_prompt,
"messages": [{"role": "user", "content": "请帮我写一个计算两个数之和的Python函数"}]
}
)
完整示例:角色参数化的AI助手
下面是一个完整的FastAPI应用示例,展示了如何实现一个具有可配置角色参数的AI助手:
python
复制
from fastapi import FastAPI, HTTPException
import httpx
from typing import Dict, List, Optional
app = FastAPI()
# 定义消息结构
class Message(BaseModel):
role: str # "user" 或 "assistant"
content: str
# 定义请求体结构
class ChatRequest(BaseModel):
model: str = "deepseek-chat"
system_prompt: str = "你是一个专业的AI助手"
user_prompt: str = "你好,请帮我回答一个问题"
messages: Optional[List[Message]] = None
@app.post("/chat")
async def chat_endpoint(request: ChatRequest):
try:
# 准备Ollama API请求参数
ollama_messages = request.messages or []
# 添加用户提示到消息列表
ollama_messages.append({"role": "user", "content": request.user_prompt})
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": request.model,
"system": request.system_prompt,
"messages": ollama_messages
}
)
# 检查响应是否成功
if response.status_code != 200:
raise HTTPException(
status_code=response.status_code,
detail=f"Ollama API调用失败: {response.text}"
)
# 提取生成的响应
generated_response = response.json()["response"]
# 返回完整的对话记录和AI助手的响应
return {
"status": "success",
"response": generated_response,
"full_dialogue": ollama_messages + [{"role": "assistant", "content": generated_response}]
}
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"处理请求时出错: {str(e)}"
)
高级角色参数设定技巧
1. 动态角色参数调整
可以根据用户需求动态调整AI角色参数:
python
复制
@app.post("/dynamic-chat")
async def dynamic_chat_endpoint(request: ChatRequest):
# 根据用户请求动态调整系统提示
if "编程" in request.user_prompt or "代码" in request.user_prompt:
system_prompt = "你是一个专业的程序员,擅长使用代码解决用户的问题。"
elif "翻译" in request.user_prompt:
system_prompt = "你是一个专业的翻译员,擅长将英文翻译成指定的语言。"
else:
system_prompt = "你是一个专业的AI助手,擅长回答各种问题。"
# 继续使用之前的API调用逻辑
# ...
2. 基于上下文的角色记忆
实现具有上下文记忆功能的AI助手,能够记住之前的对话内容:
python
复制
from fastapi import FastAPI, HTTPException
import httpx
from typing import Dict, List, Optional
from pydantic import BaseModel
class Message(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
model: str = "deepseek-chat"
user_prompt: str = "你好,请帮我回答一个问题"
messages: Optional[List[Message]] = None
class ChatState(BaseModel):
messages_history: List[Message] = []
current_role: str = "通用AI助手"
# 使用字典保存会话状态
chat_states: Dict[str, ChatState] = {}
@app.post("/memory-chat")
async def memory_chat_endpoint(session_id: str, request: ChatRequest):
try:
# 初始化会话状态
if session_id not in chat_states:
chat_states[session_id] = ChatState()
state = chat_states[session_id]
# 根据上下文自动调整角色
if "编程" in request.user_prompt or "代码" in request.user_prompt:
state.current_role = "专业程序员"
system_prompt = "你是一个专业的程序员,擅长使用代码解决用户的问题。"
elif "翻译" in request.user_prompt:
state.current_role = "专业翻译员"
system_prompt = "你是一个专业的翻译员,擅长将英文翻译成指定的语言。"
else:
state.current_role = "通用AI助手"
system_prompt = "你是一个专业的AI助手,擅长回答各种问题。"
# 更新消息历史
state.messages_history.append({"role": "user", "content": request.user_prompt})
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": request.model,
"system": system_prompt,
"messages": state.messages_history
}
)
if response.status_code != 200:
raise HTTPException(
status_code=response.status_code,
detail=f"Ollama API调用失败: {response.text}"
)
generated_response = response.json()["response"]
# 添加AI助手的响应到消息历史
state.messages_history.append({"role": "assistant", "content": generated_response})
return {
"status": "success",
"response": generated_response,
"current_role": state.current_role,
"message_count": len(state.messages_history)
}
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"处理请求时出错: {str(e)}"
)
3. 使用模板引擎
Ollama支持模板引擎,可以在系统提示中使用变量:
python
复制
system_prompt_template = """
你是一个专业的{specialty},擅长{skills}。
你需要按照以下步骤工作:
1. {step1}
2. {step2}
3. {step3}
"""
# 渲染模板
from string import Template
system_prompt = Template(system_prompt_template).substitute(
specialty="AI助手",
skills="回答各种问题",
step1="分析用户请求",
step2="使用可用工具解决问题",
step3="提供清晰简洁的回答"
)
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": "deepseek-chat",
"system": system_prompt,
"messages": [{"role": "user", "content": "请告诉我2024年的诺贝尔物理学奖获得者是谁"}]
}
)
最佳实践
1. 角色参数设计原则
- 明确性:角色定义要清晰明确,避免模糊不清
- 一致性:确保AI在对话过程中保持一致的角色
- 灵活性:设计具有一定灵活性的角色,能够处理多种类型的问题
- 安全性:确保角色参数不会导致不安全或不适当的输出
2. 错误处理和日志记录
在实际应用中,应该实现完善的错误处理和日志记录机制:
python
复制
from fastapi import FastAPI, HTTPException
import httpx
import logging
from datetime import datetime
# 配置日志记录
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
@app.post("/robust-chat")
async def robust_chat_endpoint(request: ChatRequest):
try:
start_time = datetime.now()
logger.info(f"开始处理请求,模型: {request.model}, 用户提示: {request.user_prompt}")
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:11434/api/chat",
json={
"model": request.model,
"system": request.system_prompt,
"messages": request.messages or []
},
timeout=10.0 # 设置超时时间
)
if response.status_code != 200:
logger.error(f"Ollama API调用失败,状态码: {response.status_code}, 响应: {response.text}")
raise HTTPException(
status_code=response.status_code,
detail=f"Ollama API调用失败: {response.text}"
)
processing_time = (datetime.now() - start_time).total_seconds()
logger.info(f"请求处理完成,耗时: {processing_time:.2f}秒")
return {"result": response.json()["response"]}
except httpx.exceptions.Timeout:
logger.error("Ollama API调用超时")
raise HTTPException(status_code=504, detail="Ollama API调用超时")
except httpx.exceptions.NetworkError:
logger.error("Ollama API网络错误")
raise HTTPException(status_code=502, detail="Ollama API网络错误")
except Exception as e:
logger.error(f"处理请求时出错: {str(e)}")
raise HTTPException(
status_code=500,
detail=f"处理请求时出错: {str(e)}"
)
3. 性能优化
对于高性能要求的应用,可以考虑以下优化措施:
- 使用连接池管理Ollama API连接
- 实现请求缓存机制,减少重复计算
- 根据系统负载动态调整并发请求数量
- 定期清理不再使用的会话状态
结论
通过合理设置AI提示词的角色参数,可以显著提升基于FastAPI和Ollama构建的AI应用的表现和用户体验。本文详细介绍了如何在FastAPI应用中通过Ollama API设置和使用AI模型的角色参数,包括系统提示词、用户提示词和多轮对话管理,并提供了多种角色参数设定的示例和最佳实践。
通过这些技术,开发者可以创建具有专业领域知识和特定行为模式的AI助手,满足各种应用场景的需求。希望本文能为开发者在构建AI应用时提供有价值的参考和指导。
参考资料
分享
即将为你打开全新对话,一起畅聊新话题!
新建对话
以上内容均由AI生成, 仅供参考和