Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tool_format parameter to rewrite templates for different function call formats. #4417

Merged
merged 4 commits into from
Jun 24, 2024

Conversation

mMrBun
Copy link
Contributor

@mMrBun mMrBun commented Jun 21, 2024

What does this PR do?

Added the tool_format parameter to rewrite templates for different function call formats.

Test command

GLM-4

  • hf_engine:
python src/api.py --model_name_or_path GLM-4-9B --template glm4 --tool_format glm4
  • vllm_engine
python src/api.py --model_name_or_path GLM-4-9B --template glm4 --infer_backend vllm --tool_format glm4
request code
import json
from openai import OpenAI

base_url = "http://127.0.0.1:8000/v1/"
client = OpenAI(api_key="-1", base_url=base_url)


def function_chat():
  messages = [{"role": "user", "content": "What's the weather like in Tokyo use celsius"}]
  tools = [
      {
          "type": "function",
          "function": {
              "name": "get_current_weather",
              "description": "Get the current weather",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {
                          "type": "string",
                          "description": "The city and state, e.g. San Francisco, CA",
                      },
                      "format": {
                          "type": "string",
                          "enum": ["celsius", "fahrenheit"],
                          "description": "The temperature unit to use. Infer this from the users location.",
                      },
                  },
                  "required": ["location", "format"],
              },
          }
      },
      {
          "type": "function",
          "function": {
              "name": "calculate_gpa",
              "description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
                      "hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
                  },
                  "required": ["grades", "hours"],
              },
          },
      }
  ]

  response = client.chat.completions.create(
      model="glm-4",
      messages=messages,
      tools=tools,
      stream=False,
  )
  response_message = response.choices[0].message
  tool_calls = response_message.tool_calls
  if tool_calls:
      for tool_call in tool_calls:
          tool_name = tool_call.function.name
          messages.append(response_message)
          function_response = {
              "temperature": "25°C",
              "weather": "sunny",
              "location": "Tokyo",
          }
          messages.append(
              {
                  "tool_call_id": tool_call.id,
                  "role": "tool",
                  "name": tool_name,
                  "content": json.dumps(function_response, ensure_ascii=False, indent=2),
              }
          )
      second_response = client.chat.completions.create(
          model="gpt-4o",
          messages=messages
      )
      print(second_response.choices[0].message.content)


if __name__ == "__main__":
  function_chat()

Before submitting

Copy link
Owner

@hiyouga hiyouga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hiyouga hiyouga merged commit def6d28 into hiyouga:main Jun 24, 2024
1 check passed
@hiyouga hiyouga added the solved This problem has been already solved label Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solved This problem has been already solved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants