用ollama部署大模型后API的调用demo。
注意:
ollama默认只能用127.0.0.1这个IP访问。
解除这个限制的步骤:
1、打开/etc/systemd/system/ollama.service
在[Service]添加:
注意:
ollama默认只能用127.0.0.1这个IP访问。
解除这个限制的步骤:
1、打开/etc/systemd/system/ollama.service
在[Service]添加:
Environment="OLLAMA_HOST=0.0.0.0:11434"
2、在终端执行:
sudo systemctl daemon-reload
sudo systemctl restart ollama
3、这时在浏览器打开(我这里http://192.168.31.225:11434/api/tags)会显示已部署的大模型:
如果不能显示,检查防火墙是否开放11434端口,在终端添加后应该可以访问了。
sudo ufw allow 11434
直接上代码:
unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, fphttpclient, fpjson, jsonparser, StdCtrls, ExtCtrls, opensslsockets; type ModelList=record Model:String; parameter_size:string; size:String; end; { TForm1 } TForm1 = class(TForm) Button1: TButton; ComboBox1: TComboBox; Edit2: TEdit; Label1: TLabel; Label2: TLabel; Label3: TLabel; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure ComboBox1Change(Sender: TObject); procedure FormCreate(Sender: TObject); private public ModelLists:array of ModelList; end; var Form1: TForm1; url:string='http://localhost:11434'; implementation {$R *.lfm} { TForm1 } function LoadDeepSeekAPI(question:String;modelType:string):String; var HttpClient: TFPHttpClient; RequestBody: TStringStream; Response: String; JSONRequest, JSONResponse: TJSONObject; begin Result:=''; try // 初始化 HTTP 客户端 HttpClient := TFPHttpClient.Create(nil); RequestBody := TStringStream.Create('', TEncoding.UTF8); JSONRequest := TJSONObject.Create; try // 构建请求 JSON JSONRequest.Add('model', modelType); JSONRequest.Add('stream', false); JSONRequest.Add('messages', TJSONArray.Create([ TJSONObject.Create(['role', 'user', 'content', question]) ])); JSONRequest.Add('temperature', 0.7); // 设置请求头和 URL HttpClient.AddHeader('Content-Type', 'application/json'); RequestBody.WriteString(JSONRequest.AsJSON); HttpClient.RequestBody:=RequestBody; HttpClient.AllowRedirect := True; // 发送 POST 请求 Response := HttpClient.Post(url+'/v1/chat/completions'); // 解析响应 JSONResponse := TJSONObject(GetJSON(Response)); if JSONResponse<>nil then begin if pos('choices',Response)>0 then Result := JSONResponse.GetPath('choices[0].message.content').AsString else Result:='出错信息:'+JSONResponse.GetPath('error.message').AsString+' 出错类型:'+JSONResponse.GetPath('error.type').AsString;//+' 出错代码:'+JSONResponse.GetPath('error.code').AsString; end Else Result:='调用失败'; finally HttpClient.Free; RequestBody.Free; JSONRequest.Free; JSONResponse.Free; end; except on E: Exception do ShowMessage('请求失败: ' + E.Message); end; end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Text:= LoadDeepSeekAPI(Edit2.Text,ComboBox1.Text); end; procedure TForm1.ComboBox1Change(Sender: TObject); begin Label3.Caption:='模型大小:'+ModelLists[ComboBox1.ItemIndex].size+' 模型参数(单位10亿):'+ModelLists[ComboBox1.ItemIndex].parameter_size; end; procedure TForm1.FormCreate(Sender: TObject); var jData:TJSONData; jDataArr,jDataArr2:TJSONData; i,j:Integer; tempstr:String; HttpClient: TFPHttpClient; Response: String; begin HttpClient := TFPHttpClient.Create(nil); Response := HttpClient.Get(url+'/api/tags'); ComboBox1.Items.Clear; jData:=GetJSON(Response); jDataArr:=jData.FindPath('models'); i:=jDataArr.Count; SetLength(ModelLists,i); for j:=0 to i-1 do //列出已部署的大模型 begin jDataArr2:=jData.FindPath('models['+j.ToString+'].model'); tempstr:=jDataArr2.AsJSON; tempstr:= tempstr.Replace('"','',[rfReplaceAll,rfIgnoreCase]); ModelLists[j].Model:=tempstr; ComboBox1.Items.Add(tempstr); jDataArr2:=jData.FindPath('models['+j.ToString+'].size'); tempstr:=jDataArr2.AsJSON; tempstr:= tempstr.Replace('"','',[rfReplaceAll,rfIgnoreCase]); ModelLists[j].size:=tempstr; jDataArr2:=jData.FindPath('models['+j.ToString+'].details.parameter_size'); tempstr:=jDataArr2.AsJSON; tempstr:= tempstr.Replace('"','',[rfReplaceAll,rfIgnoreCase]); ModelLists[j].parameter_size:=tempstr; end; Label3.Caption:='模型大小:'+ModelLists[ComboBox1.ItemIndex].size+' 模型参数(单位10亿):'+ModelLists[ComboBox1.ItemIndex].parameter_size; end; end.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2023-02-19 让lazarus更好支持中文