Skip to content

feat: add function and tools support for Gemini#1358

Merged
songquanpeng merged 4 commits intosongquanpeng:mainfrom
mxdlzg:gemini-dev
Apr 24, 2024
Merged

feat: add function and tools support for Gemini#1358
songquanpeng merged 4 commits intosongquanpeng:mainfrom
mxdlzg:gemini-dev

Conversation

@mxdlzg
Copy link
Copy Markdown
Contributor

@mxdlzg mxdlzg commented Apr 23, 2024

close #1348

我已确认该 PR 已自测通过,相关截图如下:
(此处放上测试通过的截图,如果不涉及前端改动或从 UI 上无法看出,请放终端启动成功的截图)

[SYS] 2024/04/23 - 15:36:34 | One API f0.6.5-f09 started 
[SYS] 2024/04/23 - 15:36:34 | running in debug mode 
[SYS] 2024/04/23 - 15:36:34 | using MySQL as database 
[SYS] 2024/04/23 - 15:36:34 | database migration started 
[SYS] 2024/04/23 - 15:36:34 | database migrated 
[SYS] 2024/04/23 - 15:36:34 | REDIS_CONN_STRING not set, Redis is not enabled 
[SYS] 2024/04/23 - 15:36:34 | using theme berry 
[SYS] 2024/04/23 - 15:36:34 | initializing token encoders 
[SYS] 2024/04/23 - 15:36:34 | token encoders initialized 
[DEBUG] 2024/04/23 - 15:36:51 | 2024042315365195072723771002422 | request body: {
    "model": "gemini-pro",
    "temperature": 0,
    "messages": [
        {
            "role": "user",
            "content": "用户所提出的问题是<Q>根据你自己的知识库回答教育的概念</Q>"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "classify_question",
                "description": "结合对话记录及背景知识,对<Q></Q>内的用户问题进行分类,并返回对应的类型字段",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": "string",
                            "description": "问题类型。下面是几种可选的问题类型: 搜索知识库,返回:'wqre';其它,返回:'sdfa'",
                            "enum": [
                                "wqre",
                                "sdfa"
                            ]
                        }
                    },
                    "required": [
                        "type"
                    ]
                }
            }
        }
    ],
    "tool_choice": {
        "type": "function",
        "function": {
            "name": "classify_question"
        }
    }
} 
[SYS] 2024/04/23 - 15:36:51 | failed to get token encoder for model gemini-pro: no encoding for model gemini-pro, using encoder for gpt-3.5-turbo 
[DEBUG] 2024/04/23 - 15:36:51 | 2024042315365195072723771002422 | converted request: 
{"contents":[{"role":"user","parts":[{"text":"用户所提出的问题是\u003cQ\u003e根据你自己的知识库回答教育的概念\u003c/Q\u003e"}]}],"safety_settings":[{"category":"HARM_CATEGORY_HARASSMENT","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_NONE"},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"BLOCK_NONE"}],"generation_config":{},"tools":[{"function_declarations":[{"description":"结合对话记录及背景知识,对\u003cQ\u003e\u003c/Q\u003e内的用户问题进行分类,并返回对应的类型字段","name":"classify_question","parameters":{"properties":{"type":{"description":"问题类型。下面是几种可选的问题类型: 搜索知识库,返回:'wqre';其它,返回:'sdfa'","enum":["wqre","sdfa"],"type":"string"}},"required":["type"],"type":"object"}}]}]} 
[INFO] 2024/04/23 - 15:36:51 | 2024042315365195072723771002422 | user 1 has enough quota 499999979000798, trusted and no need to pre-consume 
[DEBUG] 2024/04/23 - 15:36:53 | 2024042315365195072723771002422 | response body: {
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "classify_question",
              "args": {
                "type": "wqre"
              }
            }
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ]
}

 
[GIN] 2024/04/23 - 15:36:53 | 2024042315365195072723771002422 | 200 |  2.036152357s |   xxx.xxx.146.1 |    POST /v1/chat/completions
[INFO] 2024/04/23 - 15:36:53 | 2024042315365195072723771002422 | record consume log: userId=1, channelId=4, promptTokens=43, completionTokens=0, modelName=gemini-pro, tokenName=FastChat, quota=43, content=模型倍率 1.00,分组倍率 1.00,补全倍率 3.00 

@mxdlzg mxdlzg changed the title 添加Gemini的函数调用支持 feat: Add function and tools support for Gemini Apr 24, 2024
@songquanpeng songquanpeng changed the title feat: Add function and tools support for Gemini feat: add function and tools support for Gemini Apr 24, 2024
@songquanpeng songquanpeng merged commit 779b747 into songquanpeng:main Apr 24, 2024
@songquanpeng
Copy link
Copy Markdown
Owner

Thx~

SheldonLiu0412 pushed a commit to SheldonLiu0412/one-api that referenced this pull request Jan 23, 2025
* Update model.go

* Support Gemini tool_calls.

* Fix gemini tool calls (also keep support functions).

* Fixed the problem of arguments not being stringified.

Fix panic: candidate.Content.Parts out of range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini的function call格式不太对吧?FunctionDeclarations应该是function_declarations

2 participants