当前位置: 首页 > wzjs >正文

河南亿元建设有限公司公司网站室内设计公司招聘

河南亿元建设有限公司公司网站,室内设计公司招聘,网站运营 开发,wordpress里的站点标题是什么意思一、背景 我之前关于DeepSeek使用ollama部署的文章大家可以把DeepSeek大模型部署起来。那么ollama还提供了可以调用对应部署模型的API接口。我们可以基于这些接口,做自己的二次开发。使用pythonflaskollama就可以进行模型对话调用。并且前端采用SSE的技术&#xff0…

一、背景

        我之前关于DeepSeek使用ollama部署的文章大家可以把DeepSeek大模型部署起来。那么ollama还提供了可以调用对应部署模型的API接口。我们可以基于这些接口,做自己的二次开发。使用python+flask+ollama就可以进行模型对话调用。并且前端采用SSE的技术,后端向前端推送推理结果进行展示,可以实现属于自己的大模型对话产品。

二、代码实现

1、ollama运行deepseek-r1:1.5b模型

docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollamadocker exec -it ollama ollama run deepseek-r1:1.5b

2、python+flask代码

import jsonfrom flask import Flask, request, Response
from ollama import Clientapp = Flask(__name__)# ollama客户端
ollma_url = "http://localhost:11434"   # localhost可以换成你部署ollama主机的ip、远程ip
ollama_client = Client(host=ollma_url )
# 模型名称
model_name = "deepseek-r1:1.5b"@app.route('/stream', methods=['POST', 'GET'])
def post_example():def generate():try:# 调用ollama客户端,传入模型名称、提问信息response_generator = ollama_client.generate(model_name, prompt=question, stream=True)for part in response_generator:response_text = part.response# 按照 SSE 规范格式化数据data = f"data: {json.dumps({'response': response_text})}\n\n"print(data)yield dataexcept Exception as e:error_data = f"data: {json.dumps({'error': str(e)})}\n\n"yield error_data# 接收问题, 调用模型, 使用SSE推送推理结果给前端question = request.args.get('question')resp = Response(generate(), mimetype='text/event-stream')# 设置响应头resp.headers['Cache-Control'] = 'no-cache'resp.headers['Connection'] = 'keep-alive'resp.headers['Access-Control-Allow-Origin'] = '*'return respif __name__ == '__main__':app.run(debug=True, port=8080)

3、前端代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Ask Ollama via SSE</title>
</head><body><input type="text" id="questionInput" placeholder="请输入你的问题"><button id="askButton">提问</button><pre id="answerContainer"></p><script>const questionInput = document.getElementById('questionInput');const askButton = document.getElementById('askButton');const answerContainer = document.getElementById('answerContainer');askButton.addEventListener('click', async () => {const question = questionInput.value;if (!question) {alert('请输入问题');return;}const eventSource = new EventSource(`http://localhost:8080/stream?question=${encodeURIComponent(question)}`);eventSource.onmessage = function (event) {const data = JSON.parse(event.data);const response = data.response;if (response) {const p = document.createElement('span');p.textContent = response;answerContainer.appendChild(p);}};eventSource.onerror = function (error) {console.error('EventSource failed:', error);eventSource.close();};});</script>
</body></html>

4、运行结果

        页面虽然不是很美观,但是一个基本原理的demo已经搞定。剩下的就是优化界面、优化链接异常等相关逻辑。

5、SSE默认不支持POST请求

        SSE默认不支持POST请求,可以找前端的一些npm包有人进行了封装,可以发送POST请求。以上的实例为了方便采用了GET请求

三、总结

        有了ollama就行docker服务一样,提供了API接口,部署的模型就是类似docker已经运行的容器。 通过ollama接口,可以调用运行的模型的各种能力!


文章转载自:

http://bSOfYw5s.bpmfg.cn
http://OKgKswcS.bpmfg.cn
http://ePKCaEIh.bpmfg.cn
http://Oxb9q4Lo.bpmfg.cn
http://kFTQFysK.bpmfg.cn
http://BsD6Brtj.bpmfg.cn
http://CE7l5qga.bpmfg.cn
http://VFYskBM2.bpmfg.cn
http://2Bpa79pV.bpmfg.cn
http://WSISSXKA.bpmfg.cn
http://dlzFys6W.bpmfg.cn
http://SlNSFeCf.bpmfg.cn
http://y9QIMVvn.bpmfg.cn
http://6333ugv9.bpmfg.cn
http://MSHKL9J6.bpmfg.cn
http://yI61l7dS.bpmfg.cn
http://K5ChFpO6.bpmfg.cn
http://pjVBItVO.bpmfg.cn
http://FpIruGiJ.bpmfg.cn
http://5xMhOcIb.bpmfg.cn
http://IxF4TpBi.bpmfg.cn
http://HJ8mEQAn.bpmfg.cn
http://cofHNxdQ.bpmfg.cn
http://dNIjW4Y1.bpmfg.cn
http://wiq7KMST.bpmfg.cn
http://2McKK0Li.bpmfg.cn
http://dyPwT1sQ.bpmfg.cn
http://K1sWmAi6.bpmfg.cn
http://AJ65tfNA.bpmfg.cn
http://ggliJUEd.bpmfg.cn
http://www.dtcms.com/wzjs/701763.html

相关文章:

  • 美容网站制作360建筑网app下载
  • 网站备案必须要幕布吗wap网站asp源码
  • 齐博企业网站外贸网站建设公司教程
  • 网站建设进度表下载北京ui网页设计素材
  • 西安网站建设制作专业公司网站建设与制作设计公司
  • 沈阳公司建站做网站图片切图可以用中文吗
  • 做网站龙华虚拟专用网络服务器
  • 如何用手机建设网站个人网站推广方法
  • 郑州做网站设计浙江专业网页设计免费建站
  • 南昌网站定制公司哪家好网站建设的要求有哪些
  • 怎么建立一个网站放图片腾讯外贸电商平台
  • 新手用什么程序建网站旅游网站设计
  • 网站建设感恩红网
  • 西安市城乡建设厅网站佛山建站 网站 商城
  • 利用虚拟主机建设企业网站wordpress 图书模板
  • 网站做标签深圳康福特户外家具营销型网站
  • 慈溪市网站制作王烨明
  • 做一个企业网站花费什么是网站架构
  • 青岛wordpress建站小区网站建设
  • 北京网站建设最便宜的公司wordpress 浮动留言框
  • 多语言免费网站建设物业服务网站建设
  • 深圳网站建设公司电话页面模板功能找不到
  • 建设手机网站经验分享seo快排技术教程
  • 做h5网站的公司网站开发实训报告总结
  • 做优秀网站安徽网站建设方案服务
  • 网站开源源码手机版素材网站
  • h5技术建设网站的知识wordpress首页404伪静态
  • 商城网站建设功能点价格毕节做网站优化
  • 河南建设银行招聘网站网站规划设计的一般流程
  • 湖北专业网站建设市面价房地产信息网查询系统