|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
app_deepseek.py
- # 安装库文件: `pip3 install openai`
- from openai import OpenAI
- dapi_key = "sk-520a3aea160a4370b022144722d54724"
- dapi_url = "https://api.deepseek.com"
- def OASK(ask="你是谁?"):
- client = OpenAI(api_key=dapi_key, base_url=dapi_url)
- response = client.chat.completions.create(
- model="deepseek-chat",
- messages=[
- {"role": "system", "content": ask},
- ],
- max_tokens=1024,
- temperature=0.7,
- stream=False,
- )
- res = response.choices[0].message.content
- print(res)
- return res
- OASK()
复制代码
- dapi_key = "sk-520a3aea160a4370b022144722d54724"
- dapi_url = "https://api.deepseek.com"
复制代码- def wxai():
- signature = request.args.get("signature")
- timestamp = request.args.get("timestamp")
- nonce = request.args.get("nonce")
- # echostr = request.args.get("echostr")
- if not all([signature, timestamp, nonce]):
- abort(400)
- li = [WECHAT_TOKEN, timestamp, nonce]
- li.sort()
- t_str = ''.join(li)
- sign = hashlib.sha1(t_str.encode('utf-8')).hexdigest()
- if signature != sign:
- abort(403)
- else:
- if request.method == 'GET':#GET获取数据和验证
- echostr = request.args.get('echostr')
- if not echostr:
- abort(400)
- siotpub1('Get 接收:' +echostr)
- return echostr
- elif request.method == 'POST':#数据提交交互部分,人机应答入口
- xml_s = request.data
- if not xml_s:
- abort(400)
- # 获取微信公众平台信息
- # siotpub1('接收:' + str(xml_s))#调试使用
- xml_dict = xmltodict.parse(xml_s)
- xml_dict = xml_dict.get('xml')
- msg_type = xml_dict.get('MsgType')
- Content = xml_dict.get('Content')
- # 各个成员功能入口
- if msg_type == 'text':
- # Content = '\n'+wxai(Content,'\n')+'\n********37梦想********'
- Content = ''+OASK(Content,'\n')#+'**37创客 安全锋速(接送)**'
- elif msg_type =='image':
- mediaId = xml_dict.get('MediaId')
- PicUrl = xml_dict.get('PicUrl')
- Content = "图片信息" + mediaId +'\n'+ PicUrl
- res_dict = makeimg(xml_dict, mediaId)
- siotpub1('发送:'+Content)#str(res_dict))
- res_xml_str = xmltodict.unparse(res_dict)
- return res_xml_str
- elif msg_type =='voice':
- Content = "语音信息"
- elif msg_type == 'video':
- Content = "视频信息"
- elif msg_type == 'music':
- Content = "音乐信息"
- elif msg_type == 'news':
- Content = "图文信息"
- elif msg_type == 'location':
- Content = "位置信息"
- elif msg_type == 'link':
- Content = "网址链接信息"
- elif msg_type == 'shortvideo':
- Content = "短视频信息"
- elif msg_type == 'event':
- event = xml_dict.get('Event')
- if event == 'subscribe':
- Content = WEL #config配置文件中设置关注欢迎词
- elif event == 'subscribe':
- Content = '取消订阅我们'
- else:
- Content = '其他奥利给混进来了,大家开启防御系统!'
- res_dict = make(xml_dict, Content)
- siotpub1(''+Content)#str(res_dict))
- res_xml_str = xmltodict.unparse(res_dict)
- return res_xml_str
复制代码
app_deepseek.py
- #文件名字:app_deepseek.py
- import requests
- import json
- from typing import Dict, Any
- class DeepSeekClient:
- def __init__(self, api_key: str):
- self.api_key = api_key
- self.base_url = "https://api.deepseek.com/v1"
- self.headers = {
- "Authorization": f"Bearer {self.api_key}",
- "Content-Type": "application/json"
- }
- def chat_completion(self, messages: list, model: str = "deepseek-chat", temperature: float = 0.7) -> Dict[str, Any]:
- """与DeepSeek AI进行对话"""
- endpoint = f"{self.base_url}/chat/completions"
- payload = {
- "model": model,
- "messages": messages,
- "temperature": temperature
- }
- try:
- response = requests.post(endpoint, headers=self.headers, json=payload)
- response.raise_for_status()
- return response.json()
- except requests.exceptions.RequestException as e:
- print(f"API请求失败: {e}")
- return {}
- def main():
- # 从配置文件读取API密钥
- with open("config.json") as f:
- config = json.load(f)
- client = DeepSeekClient(config["deepseek_api_key"])
- # 示例对话
- messages = [
- {"role": "user", "content": "马德堡半球实验关系到的知识点有哪些?"}#你好,请介绍一下你自己
- ]
- response = client.chat_completion(messages)
- if response:
- print("AI回复:", response["choices"][0]["message"]["content"])
- if __name__ == "__main__":
- main()
复制代码
api key
sk-cb441140e1a44a4a86a725260f81be8d
config.json
- {
- "deepseek_api_key": "you api key",
- "tables": {
- "example_table": {
- "display_name": "示例表",
- "fields": [
- {
- "name": "id",
- "type": "integer",
- "widget": "Label",
- "readonly": true
- },
- {
- "name": "name",
- "type": "text",
- "widget": "Entry",
- "required": true,
- "max_length": 50
- },
- {
- "name": "created_at",
- "type": "datetime",
- "widget": "DateEntry",
- "required": true
- }
- ],
- "actions": ["create", "read", "update", "delete"]
- }
- },
- "ui_settings": {
- "theme": "cosmo",
- "window_size": "800x600",
- "background_image": "background.jpg"
- }
- }
复制代码
|
|