通过Python调用deepseek的API进行对话

liftword1个月前 (02-09)技术文章17
import requests
import json

# DeepSeek 模型的 API 端点
API_URL = "https://api.deepseek.com/v1/chat/completions"

# 你的 API 密钥
API_KEY = ""

# 定义提示词
prompt = [
    {"role": "system", "content": "你是一个翻译助手,帮助用户翻译。"},
    {"role": "user", "content": '''Spreadsheets are used to calculate project cost estimates. Totals for each cost category are then keyed into the job-costing system. What is
the BEST control to ensure that data is accurately entered into the system?
A. Display back of project detail after entry
B. Reconciliation of total amounts by project
C. Reasonableness checks for each cost type
D. Validity checks, preventing entry of character data '''}
]

# 请求体
payload = {
    "model": "deepseek-chat",  # 模型名称
    "messages": prompt,  # 提示词
    "max_tokens": 1000,  # 最大生成 token 数
    "temperature": 0.7  # 控制生成文本的随机性
}

# 请求头
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_KEY}"
}

# 发送请求
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))

# 解析响应
if response.status_code == 200:
    result = response.json()
    assistant_reply = result["choices"][0]["message"]["content"]
    print("助手回复:", assistant_reply)
else:
    print("请求失败,状态码:", response.status_code)
    print("错误信息:", response.text)


相关文章

13.python学习笔记-API调用_python通过api获取数据

Web API是网站的一部分,分为客户端和服务端。客户端通过向特定URL发送请求到服务端,服务端响应客户端的请求返回数据,这种请求方式称为API调用。目前Web项目返回数据格式常用JSON。本章将介绍...

C# 中调用 Python 代码的实现方式

一、引言在软件开发中,经常会遇到需要结合不同编程语言优势的场景。C# 和 Python 作为两种广泛应用的语言,各自在某些领域有着独特的优势。C# 以其高性能和强大的类型系统在企业级应用中占据重要地位...

python调用热门的Kimi:利用 API 生成文本的强大工具

代码概述下面给定的 Python 代码展示了如何利用 Moonshot AI API 的 Kimi 模型生成文本。Kimi 是一个由 Moonshot AI 开发的多模态 AI 模型,擅长生成中文和英...

使用python调用文心一言API,实现批量提问

在使用文心一言网页版时,一般都是输入一个提问,文心给出该提问的结果。但是,有时候我们需要批量向文心进行提问,而且这些提问的性质都是一样的,那么如何实现这个需求呢?一、提出问题例如:有一个表格,1000...

使用python调用ChatGPT的API,打造属于自己的桌面智能助手

上期图文教程,我们介绍了ChatGPT的注册使用过程,并且使用ChatGPT生成了一个CNN卷积神经网络的代码,由于ChatGPT的官方只公布了GPT-3的API接口,因此我们基于ChatGPT 3代...