main
py 129 lines 4.08 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """To run this example, you can start the vLLM server
4 without any specific flags:
5
6 ```bash
7 vllm serve unsloth/Llama-3.2-1B-Instruct \
8 --structured-outputs-config.backend outlines
9 ```
10
11 This example demonstrates how to generate chat completions
12 using the OpenAI Python client library.
13 """
14
15 from openai import OpenAI
16
17 # Modify OpenAI's API key and API base to use vLLM's API server.
18 openai_api_key = "EMPTY"
19 openai_api_base = "http://localhost:8000/v1"
20
21 tools = [
22 {
23 "type": "function",
24 "function": {
25 "name": "get_current_weather",
26 "description": "Get the current weather in a given location",
27 "parameters": {
28 "type": "object",
29 "properties": {
30 "city": {
31 "type": "string",
32 "description": "The city to find the weather for"
33 ", e.g. 'San Francisco'",
34 },
35 "state": {
36 "type": "string",
37 "description": (
38 "the two-letter abbreviation for the state that the "
39 "city is in, e.g. 'CA' which would mean 'California'"
40 ),
41 },
42 "unit": {
43 "type": "string",
44 "description": "The unit to fetch the temperature in",
45 "enum": ["celsius", "fahrenheit"],
46 },
47 },
48 "required": ["city", "state", "unit"],
49 },
50 },
51 },
52 {
53 "type": "function",
54 "function": {
55 "name": "get_forecast",
56 "description": "Get the weather forecast for a given location",
57 "parameters": {
58 "type": "object",
59 "properties": {
60 "city": {
61 "type": "string",
62 "description": (
63 "The city to get the forecast for, e.g. 'New York'"
64 ),
65 },
66 "state": {
67 "type": "string",
68 "description": (
69 "The two-letter abbreviation for the state, e.g. 'NY'"
70 ),
71 },
72 "days": {
73 "type": "integer",
74 "description": "Number of days to get the forecast for (1-7)",
75 },
76 "unit": {
77 "type": "string",
78 "description": "The unit to fetch the temperature in",
79 "enum": ["celsius", "fahrenheit"],
80 },
81 },
82 "required": ["city", "state", "days", "unit"],
83 },
84 },
85 },
86 ]
87
88 messages = [
89 {"role": "user", "content": "Hi! How are you doing today?"},
90 {"role": "assistant", "content": "I'm doing well! How can I help you?"},
91 {
92 "role": "user",
93 "content": "Can you tell me what the current weather is in Dallas \
94 and the forecast for the next 5 days, in fahrenheit?",
95 },
96 ]
97
98
99 def main():
100 client = OpenAI(
101 # defaults to os.environ.get("OPENAI_API_KEY")
102 api_key=openai_api_key,
103 base_url=openai_api_base,
104 )
105
106 models = client.models.list()
107 model = models.data[0].id
108
109 chat_completion = client.chat.completions.create(
110 messages=messages,
111 model=model,
112 tools=tools,
113 tool_choice="required",
114 stream=True, # Enable streaming response
115 )
116
117 for chunk in chat_completion:
118 if chunk.choices and chunk.choices[0].delta.tool_calls:
119 print(chunk.choices[0].delta.tool_calls)
120
121 chat_completion = client.chat.completions.create(
122 messages=messages, model=model, tools=tools, tool_choice="required"
123 )
124
125 print(chat_completion.choices[0].message.tool_calls)
126
127
128 if __name__ == "__main__":
129 main()