ollama通过API调用

AI ollama 文章 2024-06-11 08:29 256 0 全屏看文

AI助手支持GPT4.0

image.png

官方git地址:https://github.com/ollama/ollama/blob/main/docs/api.md

参数

  • model:(必填)模型名称

  • prompt:生成响应的提示

  • images:(可选)base64编码图像列表(用于多模态模型,例如llava)

高级参数(可选):

  • format:返回响应的格式。目前唯一接受的值是json

  • options: Modelfile文档中列出的其他模型参数,例如temperature

  • system:系统消息(覆盖 中定义的内容Modelfile)

  • template:要使用的提示模板(覆盖 中定义的内容Modelfile)

  • context:从上一次请求返回的上下文参数/generate,可用于保存简短的对话记忆

  • stream:如果false响应将作为单个响应对象返回,而不是对象流

  • raw:如果true提示不应用任何格式。raw如果您在向 API 发出的请求中指定了完整的模板提示,则可以选择使用该参数

  • keep_alive:控制模型在请求后保持加载到内存中的时间(默认值5m:)

JSON 模式

format通过将参数设置为 来启用 JSON 模式json。这会将响应构造为有效的 JSON 对象。请参阅下面的 JSON 模式示例

注意:指示模型在 中使用 JSON 非常重要prompt。否则,模型可能会产生大量空白。

例子

生成请求(流式传输)

要求

curl http://localhost:11434/api/generate -d '{  "model": "llama3",  "prompt": "Why is the sky blue?"}'
回复

返回 JSON 对象流:

{  "model": "llama3",  "created_at": "2023-08-04T08:52:19.385406455-07:00",  "response": "The",  "done": false}

流中的最终响应还包括有关生成的其他数据:

  • total_duration:生成响应所花费的时间

  • load_duration:加载模型所花费的时间(以纳秒为单位)

  • prompt_eval_count:提示中的标记数

  • prompt_eval_duration:执行提示所花费的时间(以纳秒为单位)

  • eval_count:响应中的标记数

  • eval_duration:生成响应所用的时间(以纳秒为单位)

  • context:此响应中使用的对话编码,可在下一个请求中发送以保留对话记忆

  • response:如果响应是流式传输的,则为空;如果响应不是流式传输的,则将包含完整响应

要计算每秒生成令牌数 (token/s) 的响应速度,请除以eval_count/ eval_duration* 10^9。

{  "model": "llama3",  "created_at": "2023-08-04T19:22:45.499127Z",  "response": "",  "done": true,  "context": [1, 2, 3],  "total_duration": 10706818083,  "load_duration": 6338219291,  "prompt_eval_count": 26,  "prompt_eval_duration": 130079000,  "eval_count": 259,  "eval_duration": 4232710000}

请求(无流媒体)

要求

当流媒体关闭时,一次回复即可收到响应。

curl http://localhost:11434/api/generate -d '{  "model": "llama3",  "prompt": "Why is the sky blue?",  "stream": false}'
回复

如果stream设置为false,响应将是单个 JSON 对象:

{  "model": "llama3",  "created_at": "2023-08-04T19:22:45.499127Z",  "response": "The sky is blue because it is the color of the sky.",  "done": true,  "context": [1, 2, 3],  "total_duration": 5043500667,  "load_duration": 5025959,  "prompt_eval_count": 26,  "prompt_eval_duration": 325953000,  "eval_count": 290,  "eval_duration": 4709213000}

请求(JSON 模式)

当format设置为时json,输出将始终是格式正确的 JSON 对象。指示模型以 JSON 格式响应也很重要。

要求

curl http://localhost:11434/api/generate -d '{  "model": "llama3",  "prompt": "What color is the sky at different times of the day? Respond using JSON",  "format": "json",  "stream": false}'
回复

{  "model": "llama3",  "created_at": "2023-11-09T21:07:55.186497Z",  "response": "{\n\"morning\": {\n\"color\": \"blue\"\n},\n\"noon\": {\n\"color\": \"blue-gray\"\n},\n\"afternoon\": {\n\"color\": \"warm gray\"\n},\n\"evening\": {\n\"color\": \"orange\"\n}\n}\n",  "done": true,  "context": [1, 2, 3],  "total_duration": 4648158584,  "load_duration": 4071084,  "prompt_eval_count": 36,  "prompt_eval_duration": 439038000,  "eval_count": 180,  "eval_duration": 4196918000}

的值response将是一个包含类似以下内容的 JSON 的字符串:

{  "morning": {    "color": "blue"
  },  "noon": {    "color": "blue-gray"
  },  "afternoon": {    "color": "warm gray"
  },  "evening": {    "color": "orange"
  }
}

请求(带图片)

要将图像提交到多模式模型(如llava或 )bakllava,请提供 base64 编码的 列表images:

要求

curl http://localhost:11434/api/generate -d '{  "model": "llava",  "prompt":"What is in this picture?",  "stream": false,  "images": ["iVBORw0KGgoAAAANSUhEUgAAAG0AAABmCAYAAADBPx+VAAAACXBIWXMA"]}'

回复

{
  "model": "llava",
  "created_at": "2023-11-03T15:36:02.583064Z",
  "response": "A happy cartoon character, which is cute and cheerful.",
  "done": true,
  "context": [1, 2, 3],
  "total_duration": 2938432250,
  "load_duration": 2559292,
  "prompt_eval_count": 1,
  "prompt_eval_duration": 2195557000,
  "eval_count": 44,
  "eval_duration": 736432000
}


-EOF-

AI助手支持GPT4.0