forked from EricLBuehler/mistral.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_model_example.py
More file actions
219 lines (172 loc) · 7.12 KB
/
multi_model_example.py
File metadata and controls
219 lines (172 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Example demonstrating multi-model usage with mistral.rs Python bindings.
This example shows how to:
1. Load multiple models
2. List available models
3. Switch between models for different requests
4. Manage default model selection
"""
from mistralrs import (
Runner,
Which,
ChatCompletionRequest,
Architecture,
MultiModelRunner,
)
# Example 1: Using MultiModelRunner wrapper for cleaner API
def example_multi_model_runner():
"""Demonstrate using the MultiModelRunner for managing multiple models."""
# First, create a regular runner with one model
runner = Runner(
which=Which.Plain(
model_id="microsoft/DialoGPT-small",
arch=Architecture.Gpt2,
num_device_layers=None,
)
)
# Convert to MultiModelRunner for multi-model operations
multi_runner = MultiModelRunner(runner)
# List available models (should show just the initial model)
print("Available models:", multi_runner.list_models())
# Send a request to a specific model
messages = [{"role": "user", "content": "Hello, how are you?"}]
request = ChatCompletionRequest(messages=messages)
# Use the specific model ID from list_models()
model_ids = multi_runner.list_models()
if model_ids:
response = multi_runner.send_chat_completion_request_to_model(
request=request,
model_id=model_ids[0], # Use first available model
)
print(f"Response from {model_ids[0]}:", response.choices[0].message.content)
# Check and set default model
default = multi_runner.get_default_model_id()
print(f"Default model: {default}")
# Send request to default model (no model_id specified)
response = multi_runner.send_chat_completion_request(request=request)
print("Response from default model:", response.choices[0].message.content)
# Example 2: Using regular Runner with model_id parameter
def example_runner_with_model_id():
"""Demonstrate using regular Runner with model_id in requests."""
# Create a runner (in a real multi-model setup, this would have multiple models loaded)
runner = Runner(
which=Which.Plain(
model_id="microsoft/DialoGPT-small",
arch=Architecture.Gpt2,
)
)
# List available models
model_ids = runner.list_models()
print("Available models:", model_ids)
# Get default model
default_model = runner.get_default_model_id()
print(f"Default model: {default_model}")
# Send request with specific model_id
messages = [{"role": "user", "content": "Tell me a joke"}]
request = ChatCompletionRequest(messages=messages)
if model_ids:
# Request to specific model
response = runner.send_chat_completion_request(
request=request, model_id=model_ids[0]
)
print(f"Response from {model_ids[0]}:", response.choices[0].message.content)
# Request without model_id (uses default)
response = runner.send_chat_completion_request(request=request)
print("Response from default model:", response.choices[0].message.content)
# Example 3: Working with different types of models
def example_mixed_model_types():
"""Example showing how different model types could be used in multi-model setup."""
# In a real scenario, you would load multiple models through configuration
# This example shows the API usage pattern
# Create a text generation model
text_runner = Runner(
which=Which.Plain(
model_id="gpt2",
arch=Architecture.Gpt2,
)
)
multi_runner = MultiModelRunner(text_runner)
# Different types of requests to different models
# Text generation request
text_messages = [{"role": "user", "content": "Write a short poem about AI"}]
text_request = ChatCompletionRequest(messages=text_messages)
model_ids = multi_runner.list_models()
if model_ids:
response = multi_runner.send_chat_completion_request_to_model(
request=text_request, model_id=model_ids[0]
)
print(f"Text model response:\n{response.choices[0].message.content}")
# In a real multi-model setup with vision models loaded, you could do:
# vision_messages = [{"role": "user", "content": [
# {"type": "text", "text": "What's in this image?"},
# {"type": "image_url", "image_url": {"url": "path/to/image.jpg"}}
# ]}]
# vision_request = ChatCompletionRequest(messages=vision_messages)
# response = multi_runner.send_chat_completion_request_to_model(
# request=vision_request,
# model_id="vision-model-id"
# )
# Example 4: Model management operations
def example_model_management():
"""Demonstrate model management operations."""
runner = Runner(
which=Which.Plain(
model_id="gpt2",
arch=Architecture.Gpt2,
)
)
# List models
print("Initial models:", runner.list_models())
# Get and set default model
current_default = runner.get_default_model_id()
print(f"Current default model: {current_default}")
# In a multi-model setup, you could change the default
model_ids = runner.list_models()
if model_ids and len(model_ids) > 1:
# Set a different model as default
runner.set_default_model_id(model_ids[1])
print(f"Changed default model to: {model_ids[1]}")
# Remove a model (in multi-model setup)
# Note: Be careful not to remove all models or the currently active one
# if len(model_ids) > 1:
# runner.remove_model(model_ids[0])
# print(f"Removed model: {model_ids[0]}")
# print("Remaining models:", runner.list_models())
# Example 5: Streaming with specific models
def example_streaming_with_models():
"""Demonstrate streaming responses from specific models."""
runner = Runner(
which=Which.Plain(
model_id="gpt2",
arch=Architecture.Gpt2,
)
)
multi_runner = MultiModelRunner(runner)
messages = [{"role": "user", "content": "Tell me a long story"}]
request = ChatCompletionRequest(messages=messages, stream=True)
model_ids = multi_runner.list_models()
if model_ids:
# Stream from specific model
stream = multi_runner.send_chat_completion_request_to_model(
request=request, model_id=model_ids[0]
)
print(f"Streaming from {model_ids[0]}:")
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print() # New line after streaming
if __name__ == "__main__":
print("=== Multi-Model Example 1: MultiModelRunner ===")
example_multi_model_runner()
print("\n" + "=" * 50 + "\n")
print("=== Multi-Model Example 2: Runner with model_id ===")
example_runner_with_model_id()
print("\n" + "=" * 50 + "\n")
print("=== Multi-Model Example 3: Mixed Model Types ===")
example_mixed_model_types()
print("\n" + "=" * 50 + "\n")
print("=== Multi-Model Example 4: Model Management ===")
example_model_management()
print("\n" + "=" * 50 + "\n")
print("=== Multi-Model Example 5: Streaming ===")
example_streaming_with_models()