手写python调用Deepseek接口,测试

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 实现从文本文件提取数据并分析保存

一、引言在日常的数据处理工作中,我们经常会遇到从文本文件中提取特定信息并进行分析的需求。本文将详细介绍如何使用 Python 编写代码,从一个包含用户网络使用信息的文本文件中提取用户姓名、入站流量和出...

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

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

Python:读取文本返回关键词及其权重

使用 jieba.analyse 库 函数 extract_tags( )从文本 data 中提取关键词,并返回关键词及其权重。参数:data:待分析的文本(字符串)。topK=10:提取权重最高的前...

Python读写docx文件

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

Python读写文本数据

问题你需要读写各种不同编码的文本数据,比如 ASCII,UTF-8或UTF-16编码等。解决方案使用带有 rt 模式的 open()函数读取文本文件。如下所示:# Read the entire fi...

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

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