ollama ``` glm4模型函数调用支持 ```

0ve6wy6x  于 23天前  发布在  其他
关注(0)|答案(5)|浏览(18)

glm4模型支持函数调用,但模型库中的模板不支持。

ygya80vv

ygya80vv1#

FROM glm4:9b

SYSTEM """
你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。
"""

TEMPLATE """
[gMASK]<sop>
{{ if .Messages }}
{{- if or .System .Tools }}<|system|>
{{- if .System }}

{{ .System }}
{{- end }}
{{- if .Tools }}

你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。当您收到函数调用请求时,使用输出格式化原始使用问题的答案。

{{- end }}
{{- end }}<|eot_id|>
{{- $lastUserIdx := -1 }}

{{- range $i, $_ := .Messages }}
{{- with eq .Role "user" }}
{{- $lastUserIdx = $i }}{{ end }}
{{- end }}

{{ $lastUserIdx }}

{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 }}
{{- if eq .Role "user" }}<|user|>
{{- if and $.Tools (eq $i $lastUserIdx) }}

给定以下函数,请为函数调用返回一个JSON,其中包含最能回答给定提示的正确参数。

以以下格式响应 {"name": function name, "parameters": dictionary of argument name and its value}。 不要使用变量。

{{ $.Tools }}
{{- end }}

{{ .Content }}<|eot_id|>{{ if $last }}<|assistant|>

{{ end }}
{{- else if eq .Role "assistant" }}<|assistant|>
{{- if .ToolCalls }}

{{- range .ToolCalls }}{"name": "{{ .Function.Name }}", "parameters": {{ .Function.Arguments }}}{{ end }}
{{- else }}

{{ .Content }}{{ if not $last }}<|eot_id|>{{ end }}
{{- end }}
{{- else if eq .Role "tool" }}Tools:

{{ .Content }}<|eot_id|>{{ if $last }}<|assistant|>

{{ end }}
{{- end }}
{{- end }}
{{ if .System }}<|system|>
{{ .System }}{{ end }}{{ if .Prompt }}<|user|>
{{ .Prompt }}{{ end }}<|assistant|>
{{ .Response }}
{{ end }}
"""

PARAMETER stop "<|system|>"
PARAMETER stop "<|user|>"
PARAMETER stop "<|assistant|>"
PARAMETER stop "<|eot_id|>"
ghhaqwfi

ghhaqwfi2#

抱歉,这种代码太复杂了,有人能提供支持吗?

de90aj5v

de90aj5v3#

这是一个为glm4添加工具支持的模型文件。将其保存在名为Modelfile的文件中,然后创建一个新模型:

ollama create glm4-tool:9b -f Modelfile

接着使用工具调用它:

$ curl -s localhost:11434/v1/chat/completions -d '{"model": "glm4-tool:9b","tools":[{"type":"function","function": {}}], "messages": [{"role":"user","content":"weather in zurich"}], "stream": false}' | jq
{
  "id": "chatcmpl-595",
  "object": "chat.completion",
  "created": 1724750444,
  "model": "glm4-tool:9b",
  "system_fingerprint": "fp_ollama",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
          {
            "id": "call_2h4gimgk",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"location\":\"Zurich\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 247,
    "completion_tokens": 18,
    "total_tokens": 265
  }
}
liwlm1x9

liwlm1x94#

这是一个为 glm4 添加工具支持的 modelfile。将其保存在名为 Modelfile 的文件中,然后创建一个新模型:

ollama create glm4-tool:9b -f Modelfile

然后用工具调用它:

$ curl -s localhost:11434/v1/chat/completions -d '{"model": "glm4-tool:9b","tools":[{"type":"function","function": {}}], "messages": [{"role":"user","content":"weather in zurich"}], "stream": false}' | jq
{
  "id": "chatcmpl-595",
  "object": "chat.completion",
  "created": 1724750444,
  "model": "glm4-tool:9b",
  "system_fingerprint": "fp_ollama",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
          {
            "id": "call_2h4gimgk",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"location\":\"Zurich\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 247,
    "completion_tokens": 18,
    "total_tokens": 265
  }
}

我知道这个 modelfile 的功能,但我认为它并不完美。我需要有人对它进行一些改进。

相关问题