python调用ollamaAPI 模型名称【llama2-chinese:latest】
    		       		warning:
    		            这篇文章距离上次修改已过452天,其中的内容可能已经有所变动。
    		        
        		                
                要在Python中调用Ollama API模型,您需要使用HTTP客户端库(如requests)来发送API请求。以下是一个使用requests库调用名为llama2-chinese:latest的Ollama模型的示例代码:
首先,安装requests库(如果尚未安装):
pip install requests然后,使用以下Python代码调用Ollama API:
import requests
 
# 设置API端点
api_endpoint = "https://api.ollama.ai/v1/complete"
 
# 设置API密钥
api_key = "您的API密钥"
 
# 设置模型名称
model_name = "llama2-chinese:latest"
 
# 设置提示信息
prompt = "中国的首都是哪里?"
 
# 设置API参数
params = {
    "model": model_�name,
    "prompt": prompt,
}
 
# 发送POST请求
response = requests.post(api_endpoint, params=params, headers={"Authorization": f"Bearer {api_key}"})
 
# 检查响应并打印结果
if response.status_code == 200:
    print(response.json())
else:
    print("调用API时发生错误:", response.status_code)请确保将api_key替换为您的Ollama API密钥,并将prompt变量设置为您想要查询的内容。
注意:实际的API请求可能需要更多的参数或不同的方法来进行身份验证和使用更复杂的请求体。上述代码是一个基本示例,用于调用Ollama API的基本功能。
评论已关闭