utils.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from typing import Dict, List
  2. from openai.types.chat import (ChatCompletionMessageParam,
  3. ChatCompletionToolParam)
  4. from typing_extensions import TypedDict
  5. from tests.utils import APHRODITE_PATH
  6. class ServerConfig(TypedDict):
  7. model: str
  8. arguments: List[str]
  9. # universal args for all models go here. also good if you need to test locally
  10. # and change type or KV cache quantization or something.
  11. ARGS: List[str] = ["--enable-auto-tool-choice", "--max-model-len", "8096"]
  12. CONFIGS: Dict[str, ServerConfig] = {
  13. "hermes": {
  14. "model":
  15. "NousResearch/Hermes-3-Llama-3.1-8B",
  16. "arguments": [
  17. "--tool-call-parser", "hermes", "--chat-template",
  18. str(APHRODITE_PATH / "examples/chat_templates/hermes_tool.jinja")
  19. ]
  20. },
  21. "mistral": {
  22. "model":
  23. "mistralai/Mistral-7B-Instruct-v0.3",
  24. "arguments": [
  25. "--tool-call-parser", "mistral", "--chat-template",
  26. str(APHRODITE_PATH / "examples/chat_templates/mistral_tool.jinja"),
  27. "--ignore-patterns=\"consolidated.safetensors\""
  28. ]
  29. }
  30. }
  31. WEATHER_TOOL: ChatCompletionToolParam = {
  32. "type": "function",
  33. "function": {
  34. "name": "get_current_weather",
  35. "description": "Get the current weather in a given location",
  36. "parameters": {
  37. "type": "object",
  38. "properties": {
  39. "city": {
  40. "type":
  41. "string",
  42. "description":
  43. "The city to find the weather for, "
  44. "e.g. 'San Francisco'"
  45. },
  46. "state": {
  47. "type":
  48. "string",
  49. "description":
  50. "the two-letter abbreviation for the state "
  51. "that the city is in, e.g. 'CA' which would "
  52. "mean 'California'"
  53. },
  54. "unit": {
  55. "type": "string",
  56. "description": "The unit to fetch the temperature in",
  57. "enum": ["celsius", "fahrenheit"]
  58. }
  59. }
  60. }
  61. }
  62. }
  63. SEARCH_TOOL: ChatCompletionToolParam = {
  64. "type": "function",
  65. "function": {
  66. "name":
  67. "web_search",
  68. "description":
  69. "Search the internet and get a summary of the top "
  70. "10 webpages. Should only be used if you don't know "
  71. "the answer to a user query, and the results are likely"
  72. "to be able to be found with a web search",
  73. "parameters": {
  74. "type": "object",
  75. "properties": {
  76. "search_term": {
  77. "type":
  78. "string",
  79. "description":
  80. "The term to use in the search. This should"
  81. "ideally be keywords to search for, not a"
  82. "natural-language question"
  83. }
  84. },
  85. "required": ["search_term"]
  86. }
  87. }
  88. }
  89. MESSAGES_WITHOUT_TOOLS: List[ChatCompletionMessageParam] = [{
  90. "role":
  91. "system",
  92. "content":
  93. "You are a helpful assistant with access to tools. If a tool"
  94. " that you have would be helpful to answer a user query, "
  95. "call the tool. Otherwise, answer the user's query directly "
  96. "without calling a tool. DO NOT CALL A TOOL THAT IS IRRELEVANT "
  97. "to the user's question - just respond to it normally."
  98. }, {
  99. "role":
  100. "user",
  101. "content":
  102. "Hi! How are you?"
  103. }, {
  104. "role":
  105. "assistant",
  106. "content":
  107. "I'm doing great! How can I assist you?"
  108. }, {
  109. "role":
  110. "user",
  111. "content":
  112. "Can you tell me a joke please?"
  113. }]
  114. MESSAGES_ASKING_FOR_TOOLS: List[ChatCompletionMessageParam] = [{
  115. "role":
  116. "user",
  117. "content":
  118. "What is the weather in Dallas, Texas in Fahrenheit?"
  119. }]
  120. MESSAGES_WITH_TOOL_RESPONSE: List[ChatCompletionMessageParam] = [{
  121. "role":
  122. "user",
  123. "content":
  124. "What is the weather in Dallas, Texas in Fahrenheit?"
  125. }, {
  126. "role":
  127. "assistant",
  128. "tool_calls": [{
  129. "id": "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
  130. "type": "function",
  131. "function": {
  132. "name":
  133. WEATHER_TOOL["function"]["name"],
  134. "arguments":
  135. '{"city": "Dallas", "state": "TX", '
  136. '"unit": "fahrenheit"}'
  137. }
  138. }]
  139. }, {
  140. "role":
  141. "tool",
  142. "tool_call_id":
  143. "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
  144. "content":
  145. "The weather in Dallas is 98 degrees fahrenheit, with partly"
  146. "cloudy skies and a low chance of rain."
  147. }]
  148. MESSAGES_ASKING_FOR_PARALLEL_TOOLS: List[ChatCompletionMessageParam] = [{
  149. "role":
  150. "user",
  151. "content":
  152. "What is the weather in Dallas, Texas and Orlando, Florida in "
  153. "Fahrenheit?"
  154. }]
  155. MESSAGES_WITH_PARALLEL_TOOL_RESPONSE: List[ChatCompletionMessageParam] = [{
  156. "role":
  157. "user",
  158. "content":
  159. "What is the weather in Dallas, Texas and Orlando, Florida in "
  160. "Fahrenheit?"
  161. }, {
  162. "role":
  163. "assistant",
  164. "tool_calls": [{
  165. "id": "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
  166. "type": "function",
  167. "function": {
  168. "name":
  169. WEATHER_TOOL["function"]["name"],
  170. "arguments":
  171. '{"city": "Dallas", "state": "TX", '
  172. '"unit": "fahrenheit"}'
  173. }
  174. }, {
  175. "id": "chatcmpl-tool-d027061e1bd21cda48bee7da829c1f5b",
  176. "type": "function",
  177. "function": {
  178. "name":
  179. WEATHER_TOOL["function"]["name"],
  180. "arguments":
  181. '{"city": "Orlando", "state": "Fl", '
  182. '"unit": "fahrenheit"}'
  183. }
  184. }]
  185. }, {
  186. "role":
  187. "tool",
  188. "tool_call_id":
  189. "chatcmpl-tool-03e6481b146e408e9523d9c956696295",
  190. "content":
  191. "The weather in Dallas TX is 98 degrees fahrenheit with mostly "
  192. "cloudy skies and a chance of rain in the evening."
  193. }, {
  194. "role":
  195. "tool",
  196. "tool_call_id":
  197. "chatcmpl-tool-d027061e1bd21cda48bee7da829c1f5b",
  198. "content":
  199. "The weather in Orlando FL is 78 degrees fahrenheit with clear"
  200. "skies."
  201. }]