秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  276 随笔 :: 0 文章 :: 305 评论 :: 19万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
用ollama部署大模型后API的调用demo。
注意:
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.
复制代码

 

posted on   秋·风  阅读(132)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-02-19 让lazarus更好支持中文
点击右上角即可分享
微信分享提示