message_util.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """
  2. This module provides utility functions for working with messages in the OpenAI API.
  3. Functions:
  4. - new_message(role: str, content: str) -> dict: Creates a new message with the specified role and content.
  5. - system_message(content: str) -> dict: Creates a system message with the specified content.
  6. - user_message(content: str) -> dict: Creates a user message with the specified content.
  7. - assistant_message(content: str) -> dict: Creates an assistant message with the specified content.
  8. - tool_calls(tool_calls) -> dict: Creates a message with assistant tool calls.
  9. - tool_call_result(id, content) -> dict: Creates a tool call result message with the specified ID and content.
  10. - is_tool_call(message: ChatCompletionMessage) -> bool: Checks if a message is a tool call.
  11. """
  12. from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageToolCall
  13. from openai.types.chat.chat_completion_message_tool_call import Function
  14. def new_message(role: str, content: str, reasoning_content=None):
  15. if role != "user" and role != "system" and role != "assistant":
  16. raise ValueError(f"Invalid role {role}")
  17. if reasoning_content is None:
  18. return {"role": role, "content": content}
  19. else:
  20. return {"role": role, "content": content, "reasoning_content": reasoning_content}
  21. def system_message(content: str):
  22. return new_message("system", content)
  23. def user_message(content: str):
  24. return new_message("user", content)
  25. def assistant_message(content: str):
  26. return new_message("assistant", content)
  27. def tool_calls(tool_calls, reasoning_content=None):
  28. if reasoning_content is None:
  29. return {"role": "assistant", "tool_calls": tool_calls}
  30. else:
  31. return {"role": "assistant", "tool_calls": tool_calls, "reasoning_content": reasoning_content}
  32. def tool_call_result(id, content):
  33. return {"role": "tool", "tool_call_id": id, "content": content}
  34. def is_tool_call(message: ChatCompletionMessage) -> bool:
  35. return bool(message.tool_calls)
  36. def merge_tool_call_delta(tool_calls, tool_call_delta):
  37. if len(tool_calls) - 1 >= tool_call_delta.index:
  38. tool_call = tool_calls[tool_call_delta.index]
  39. tool_call.function.arguments += (
  40. ""
  41. if tool_call_delta.function.arguments is None
  42. else tool_call_delta.function.arguments
  43. )
  44. else:
  45. tool_call = ChatCompletionMessageToolCall(
  46. id=tool_call_delta.id,
  47. function=Function(
  48. name=tool_call_delta.function.name,
  49. arguments=tool_call_delta.function.arguments,
  50. ),
  51. type=tool_call_delta.type,
  52. )
  53. tool_calls.append(tool_call)
  54. print(
  55. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaatttttttttttttttttttttttttttttmmmmmmmmmmmmmmmmmmmmmmmmmm"
  56. )
  57. print(tool_calls)