手写python调用Deepseek接口,测试

liftword1个月前 (05-16)技术文章11

DeepSeek 国产大模型之光。

可否实现自己编写程序,调用deepseek呢?

尝试一下。 以官方提供的例子 和 手写Web程序 两个方式,调用Deep seek api。

工具

ollama , deepseek,anaconda ,spyder等, python 版本3.9

ollama 是前期安装本机deepseek, 安装deep seek 1.5b 和 7b。笔记本上都能运行,再高速度就慢了。这次没有测试本机deepseek。

1. anaconda : python开发工具, 设置开发环境python 3.9(要求要>3.8)


启动anaconda,配置3.9开发环境。

安装需要库,

pip install openAI



2. spyder 调试工具

编写python程序和运行调试

程序1:

from openai import OpenAI

client = OpenAI(api_key='DEEPSEEK KEY', base_url="https://api.deepseek.com")

response = client.chat.completions.create(

model="deepseek-chat",

messages=[

{"role": "system", "content": "你是谁"},

{"role": "user", "content": "Hello"},

],

stream=False

)

print(response.choices[0].message.content)


其中:DEEPSEEK KEY 需要换成自己的。

从官网,可以创建自己的key。

程序2

import os

import requests

from flask import Flask, render_template_string, request

app = Flask(__name__)

# 替换为你的 DeepSeek API 密钥和 API 端点

DEEPSEEK_API_KEY = os.getenv('key')

DEEPSEEK_API_ENDPOINT = "https://api.deepseek.com/chat/completions"@app.route('/', methods=['GET', 'POST'])

def index():

answer = None

if request.method == 'POST':

question = request.form.get('question')

headers = { 'Authorization': f'Bearer {DEEPSEEK_API_KEY}', 'Content-Type': 'application/json' }

data = {

"input": question

}

"stream": False # Disable streaming

}

try:

response = requests.post(DEEPSEEK_API_ENDPOINT, headers=headers, json=data)

response.raise_for_status()

answer = response.json().get('answer')

except requests.RequestException as e:

answer = f"请求出错: {e}"

except ValueError as e:

answer = f"解析响应出错: {e}"

return render_template_string('''

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>DeepSeek 提问和问答小程序</title>

</head>

<body>

<h1>DeepSeek 问答</h1>

<form method="post">

<label for="question">请输入问题:</label><br>

<textarea id="question" name="question" rows="6" cols="50"></textarea><br>

<input type="submit" value="提交">

</form>

{% if answer %}

<h2>回答:</h2>

<p>{{ answer }}</p>

{% endif %}

</body>

</html>

''', answer=answer)

if __name__ == '__main__':

app.run(debug=True)

缩行格式没有了,请补充

效果如图所示:

实际测试这个报错了, 应该是调用地址,还是认证原因,还在调试中。

总结

使用python 语言,可以很方便调用Deepseek接口,在自己程序中实现AI应用。

你也来试试吧。

相关文章

Python处理文本的25个经典操作

Python处理文本的优势主要体现在其简洁性、功能强大和灵活性。具体来说,Python提供了丰富的库和工具,使得对文件的读写、处理变得轻而易举。简洁的文件操作接口Python通过内置的open()函数...

Python读写docx文件

Python读写docx文件Python读写word文档有现成的库可以处理pip install python-docx安装一下。https://python-docx.readthedocs.io/...

Python如何读取PDF中的文字和图片,请移步至此!

从PDF中提取内容能帮助我们获取文件中的信息,以便进行进一步的分析和处理。此外,在遇到类似项目时,提取出来的文本或图片也能再次利用。要在Python中通过代码提取PDF文件中的文本和图片,可以使用 S...

用python帮别人写了个文字识别程序

文章目录前言一.需求分析二.代码实现1.百度文字识别2.查看文档获取access_token3.图片代码4.代码部分解读三.效果展示私信小编01即可获取大量Python学习资源前言就在前几天一个大一学...

不要钱,用Python识别图片中的文字

今天我们要学习的是在Python通过调用外部应用识别图片中的文字(OCR)。我们并不需要探究艰深的图像识别的原理和算法,只需要站在巨人的肩膀上知道如何调用百度的文字识别服务就可以用二三十行代码高效实现...

Python自动化办公自学笔记(八)文件操作

一、文件使用在Python里面,文件分为两种类型,一种是文本文件,后缀名为“.txt”,由单一特定编码的字符组成;另一种是二进制文件,由比特0和比特1组成。Python对以上两种文件都有统一的操作步骤...