# 对话生成  函数式调用FunctionCall

## OpenAPI Specification

```yaml
openapi: 3.0.1
info:
  title: ''
  description: ''
  version: 1.0.0
paths:
  /v1/chat/completions:
    post:
      summary: 对话生成  函数式调用FunctionCall
      deprecated: false
      description: >
        **冰橙API提供与OPENAI官方的API兼容的接口方式，方便国内开发者进行与OPENAI的CHATGPT的接口对接服务**




        :::highlight purple 💡

        函数式最终的目的是希望通过GPT智能返回函数方法，然后将数据结果通过自然语言回复。

        1、第一步是提问GPT（同时规定回复的JSON格式或者说是函数对象）。

        2、本地根据GPT回复的JSON格式查询结果做本地化查询后，

        3、再问GPT（将本地结果再次提交）。然后GPT结合我们的查询结果用自然语言回复我们。

        :::



        - **Function calling的实现过程**

        看似复杂，实际上很简单，OpenAI已经帮我们做好了：提前定义外部函数库，在ChatCompletions.create()函数中进行对应的参数设置。

        所谓的外部函数库，可以理解为：当进行某一项应用开发时，需要用到的中间处理逻辑函数，比如你要开发一个邮件自动回复程序，你就需要定义：

        获取邮件的函数

        发送邮件的函数

        .......

        在Chat Completion 模型执行Function
        calling功能时，大模型能够充分发挥自身的语义理解能力，解析用户的输入，然后在函数库中自动挑选出最合适函数进行运行，并给出问题的答案，整个过程不需要人工手动干预。


        -  **如何正确构建外部函数库**

        虽然Chat Completion模型具有函数调用（Function
        Calling）能力，但其对函数的理解方式并不与传统编程环境下的函数相同。类似于对话中有效的Prompt设计，优化函数调用的格式和结构能显著提升模型的执行准确性。因此，明确合适的数据传入格式成为关键一步，只有当数据格式设计得当，模型才能更准确地理解和执行任务。


        - **第三方示例说明**

        https://platform.openai.com/docs/guides/function-calling/function-calling

        https://zhuanlan.zhihu.com/p/645501247

        https://new.qq.com/rain/a/20230818A045F400

        https://juejin.cn/post/7244818419601637413



        :::highlight blue 📌

        并非所有模型版本都使用函数调用数据进行训练。以下模型支持函数调用：

        gpt-4o

        gpt-4o-mini

        gpt-4-0613

        gpt-3.5-turbo

        gpt-3.5-turbo-1106

        gpt-3.5-turbo-0613

        此外，以下型号还支持并行函数调用：

        gpt-4-1106-preview

        gpt-3.5-turbo-1106

        :::



        ### token获取方式：

        - 访问公众号《冰橙云》进入菜单冰橙AI助手后，访问右上角 / API密钥 可查看 token

        - 访问：https://yewu.bcwhkj.cn 》 个人 》API密钥 》Token令牌 
      tags:
        - 文生文 对话生成
      parameters:
        - name: Content-Type
          in: header
          description: 固定值
          required: false
          example: application/json
          schema:
            type: string
        - name: Authorization
          in: header
          description: token要更换为个人自己的TOKEN
          example: Bearer token
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                model:
                  type: string
                messages:
                  type: array
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                      content:
                        type: string
                    x-apifox-orders:
                      - role
                      - content
                functions:
                  type: array
                  items:
                    type: object
                    properties:
                      name:
                        type: string
                      description:
                        type: string
                      parameters:
                        type: object
                        properties:
                          type:
                            type: string
                          properties:
                            type: object
                            properties:
                              location:
                                type: object
                                properties:
                                  type:
                                    type: string
                                  description:
                                    type: string
                                required:
                                  - type
                                  - description
                                x-apifox-orders:
                                  - type
                                  - description
                              unit:
                                type: object
                                properties:
                                  type:
                                    type: string
                                  enum:
                                    type: array
                                    items:
                                      type: string
                                required:
                                  - type
                                  - enum
                                x-apifox-orders:
                                  - type
                                  - enum
                            required:
                              - location
                              - unit
                            x-apifox-orders:
                              - location
                              - unit
                          required:
                            type: array
                            items:
                              type: string
                        required:
                          - type
                          - properties
                          - required
                        x-apifox-orders:
                          - type
                          - properties
                          - required
                    x-apifox-orders:
                      - name
                      - description
                      - parameters
              required:
                - model
                - messages
                - functions
              x-apifox-orders:
                - model
                - messages
                - functions
            example:
              messages:
                - role: system
                  content: 你是一个能干的助手.
                - role: user
                  content: 谁赢得了2020年的世界职业棒球大赛?
                - role: assistant
                  content: 洛杉矶道奇队在2020年赢得了世界职业棒球大赛冠军.
                - role: user
                  content: 它在哪里举办的?
              model: gpt-3.5-turbo
              max_tokens: 4000
              stream: false
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                  object:
                    type: string
                  created:
                    type: integer
                  model:
                    type: string
                  choices:
                    type: array
                    items:
                      type: object
                      properties:
                        index:
                          type: integer
                        message:
                          type: object
                          properties:
                            role:
                              type: string
                            content:
                              type: string
                          required:
                            - role
                            - content
                          x-apifox-orders:
                            - role
                            - content
                        finish_reason:
                          type: string
                      x-apifox-orders:
                        - index
                        - message
                        - finish_reason
                  usage:
                    type: object
                    properties:
                      prompt_tokens:
                        type: integer
                      completion_tokens:
                        type: integer
                      total_tokens:
                        type: integer
                    required:
                      - prompt_tokens
                      - completion_tokens
                      - total_tokens
                    x-apifox-orders:
                      - prompt_tokens
                      - completion_tokens
                      - total_tokens
                  system_fingerprint:
                    type: string
                required:
                  - id
                  - object
                  - created
                  - model
                  - choices
                  - usage
                  - system_fingerprint
                x-apifox-orders:
                  - id
                  - object
                  - created
                  - model
                  - choices
                  - usage
                  - system_fingerprint
              examples:
                '1':
                  summary: 成功示例
                  value:
                    id: chatcmpl-9eNCTgreBpiIWLXDrCWCxwqA05DYx
                    object: chat.completion
                    created: 1719409329
                    model: gpt-3.5-turbo-0125
                    choices:
                      - index: 0
                        message:
                          role: assistant
                          content: >-
                            20220206-未编辑-在由于COVID-19流行病的影响，2020年的世界职业棒球大赛在美国佛罗里达州的阿灵顿举行。
                        logprobs: null
                        finish_reason: stop
                    usage:
                      prompt_tokens: 101
                      completion_tokens: 60
                      total_tokens: 161
                    system_fingerprint: null
                '2':
                  summary: 密钥错误
                  value:
                    codes: 900
                    mess: 密钥错误或已过期，请通过微信访问yewu.bcwhkj.cn个人中心获取
                    data: token
                '3':
                  summary: 非法内容
                  value:
                    codes: 400
                    mess: 非法字符。请勿提交
                    data: ''
                '4':
                  summary: 内容安全
                  value:
                    codes: 402
                    mess: 提交问题涉及内容安全，请修改问题
                    data: ''
                '5':
                  summary: 额度不足
                  value:
                    codes: 300
                    mess: 当前额度不足，请充值后继续使用
                    data: ''
                '6':
                  summary: 官方API维护中
                  value:
                    error:
                      code: 502
                      message: Bad gateway.
                      param: null
                      type: cf_bad_gateway
          headers: {}
          x-apifox-name: 成功
      security: []
      x-apifox-folder: 文生文 对话生成
      x-apifox-status: released
      x-run-in-apifox: https://app.apifox.com/web/project/3216478/apis/api-124906303-run
components:
  schemas: {}
  securitySchemes: {}
servers:
  - url: https://yewu.bcwhkj.cn
    description: 正式环境
security: []

```
