test_toolcall.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # coding=utf-8
  2. # Copyright 2024 the LlamaFactory team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import json
  16. import os
  17. from typing import Sequence
  18. from openai import OpenAI
  19. from transformers.utils.versions import require_version
  20. require_version("openai>=1.5.0", "To fix: pip install openai>=1.5.0")
  21. def calculate_gpa(grades: Sequence[str], hours: Sequence[int]) -> float:
  22. grade_to_score = {"A": 4, "B": 3, "C": 2}
  23. total_score, total_hour = 0, 0
  24. for grade, hour in zip(grades, hours):
  25. total_score += grade_to_score[grade] * hour
  26. total_hour += hour
  27. return round(total_score / total_hour, 2)
  28. def main():
  29. client = OpenAI(
  30. api_key="{}".format(os.environ.get("API_KEY", "0")),
  31. base_url="http://localhost:{}/v1".format(os.environ.get("API_PORT", 8000)),
  32. )
  33. tools = [
  34. {
  35. "type": "function",
  36. "function": {
  37. "name": "calculate_gpa",
  38. "description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
  39. "parameters": {
  40. "type": "object",
  41. "properties": {
  42. "grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
  43. "hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
  44. },
  45. "required": ["grades", "hours"],
  46. },
  47. },
  48. }
  49. ]
  50. tool_map = {"calculate_gpa": calculate_gpa}
  51. messages = []
  52. messages.append({"role": "user", "content": "My grades are A, A, B, and C. The credit hours are 3, 4, 3, and 2."})
  53. result = client.chat.completions.create(messages=messages, model="test", tools=tools)
  54. if result.choices[0].message.tool_calls is None:
  55. raise ValueError("Cannot retrieve function call from the response.")
  56. messages.append(result.choices[0].message)
  57. tool_call = result.choices[0].message.tool_calls[0].function
  58. print(tool_call)
  59. # Function(arguments='{"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}', name='calculate_gpa')
  60. name, arguments = tool_call.name, json.loads(tool_call.arguments)
  61. tool_result = tool_map[name](**arguments)
  62. messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)})
  63. result = client.chat.completions.create(messages=messages, model="test", tools=tools)
  64. print(result.choices[0].message.content)
  65. # Based on the grades and credit hours you provided, your Grade Point Average (GPA) is 3.42.
  66. if __name__ == "__main__":
  67. main()