|
19 | 19 | where you want to display chat completions to the user as they are generated
|
20 | 20 | by the model.
|
21 | 21 |
|
22 |
| -Here we do not use the OpenAI Python client library, because it does not support |
23 |
| -`reasoning_content` fields in the response. |
| 22 | +Remember to check content and reasoning_content exist in `ChatCompletionChunk`, |
| 23 | +content may not exist leading to errors if you try to access it. |
24 | 24 | """
|
25 | 25 |
|
26 |
| -import json |
27 |
| - |
28 |
| -import requests |
| 26 | +from openai import OpenAI |
29 | 27 |
|
30 | 28 | # Modify OpenAI's API key and API base to use vLLM's API server.
|
31 | 29 | openai_api_key = "EMPTY"
|
32 | 30 | openai_api_base = "http://localhost:8000/v1"
|
33 | 31 |
|
34 |
| -models = requests.get( |
35 |
| - f"{openai_api_base}/models", |
36 |
| - headers={ |
37 |
| - "Authorization": f"Bearer {openai_api_key}" |
38 |
| - }, |
39 |
| -).json() |
40 |
| -model = models["data"][0]["id"] |
| 32 | +client = OpenAI( |
| 33 | + api_key=openai_api_key, |
| 34 | + base_url=openai_api_base, |
| 35 | +) |
41 | 36 |
|
42 |
| -# Streaming chat completions |
43 |
| -messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}] |
| 37 | +models = client.models.list() |
| 38 | +model = models.data[0].id |
44 | 39 |
|
45 |
| -response = requests.post( |
46 |
| - f"{openai_api_base}/chat/completions", |
47 |
| - headers={"Authorization": f"Bearer {openai_api_key}"}, |
48 |
| - json={ |
49 |
| - "model": model, |
50 |
| - "messages": messages, |
51 |
| - "stream": True |
52 |
| - }, |
53 |
| -) |
| 40 | +messages = [{"role": "user", "content": "9.11 and 9.8, which is greater?"}] |
| 41 | +stream = client.chat.completions.create(model=model, |
| 42 | + messages=messages, |
| 43 | + stream=True) |
54 | 44 |
|
55 | 45 | print("client: Start streaming chat completions...")
|
56 | 46 | printed_reasoning_content = False
|
57 | 47 | printed_content = False
|
58 |
| -# Make the streaming request |
59 |
| -if response.status_code == 200: |
60 |
| - # Process the streaming response |
61 |
| - for line in response.iter_lines(): |
62 |
| - if line: # Filter out keep-alive new lines |
63 |
| - # Decode the line and parse the JSON |
64 |
| - decoded_line = line.decode("utf-8") |
65 |
| - if decoded_line.startswith("data:"): |
66 |
| - data = decoded_line[5:].strip() # Remove "data:" prefix |
67 |
| - if data == "[DONE]": # End of stream |
68 |
| - print("\nclient: Stream completed.") |
69 |
| - break |
70 |
| - try: |
71 |
| - # Parse the JSON data |
72 |
| - chunk = json.loads(data) |
73 |
| - reasoning_content = chunk["choices"][0]["delta"].get( |
74 |
| - "reasoning_content", "") |
75 |
| - content = chunk["choices"][0]["delta"].get("content", "") |
76 | 48 |
|
77 |
| - if reasoning_content: |
78 |
| - if not printed_reasoning_content: |
79 |
| - printed_reasoning_content = True |
80 |
| - print("reasoning_content:", end="", flush=True) |
81 |
| - print(reasoning_content, end="", flush=True) |
82 |
| - elif content: |
83 |
| - if not printed_content: |
84 |
| - printed_content = True |
85 |
| - print("\ncontent:", end="", flush=True) |
86 |
| - # Extract and print the content |
87 |
| - print(content, end="", flush=True) |
88 |
| - except json.JSONDecodeError: |
89 |
| - print("Error decoding JSON:", decoded_line) |
90 |
| -else: |
91 |
| - print(f"Error: {response.status_code} - {response.text}") |
| 49 | +for chunk in stream: |
| 50 | + reasoning_content = None |
| 51 | + content = None |
| 52 | + # Check the content is reasoning_content or content |
| 53 | + if hasattr(chunk.choices[0].delta, "reasoning_content"): |
| 54 | + reasoning_content = chunk.choices[0].delta.reasoning_content |
| 55 | + elif hasattr(chunk.choices[0].delta, "content"): |
| 56 | + content = chunk.choices[0].delta.content |
| 57 | + |
| 58 | + if reasoning_content is not None: |
| 59 | + if not printed_reasoning_content: |
| 60 | + printed_reasoning_content = True |
| 61 | + print("reasoning_content:", end="", flush=True) |
| 62 | + print(reasoning_content, end="", flush=True) |
| 63 | + elif content is not None: |
| 64 | + if not printed_content: |
| 65 | + printed_content = True |
| 66 | + print("\ncontent:", end="", flush=True) |
| 67 | + # Extract and print the content |
| 68 | + print(content, end="", flush=True) |
0 commit comments