Skip to content

Commit aeaf011

Browse files
Fixed response schema for vllm (#116)
* vllm integration - added vllm openai integration - added vllm agent usage example - fixed retry_count bug * made openai_vllm required * exporting ImageMessage * added OpenAIVLLM to flo_ai init * fixed response_format for openai and vllmopenai * examples -> output_schema 'name' changed to 'title'
1 parent 11b2c8f commit aeaf011

4 files changed

Lines changed: 28 additions & 17 deletions

File tree

flo_ai/examples/output_formatter.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def main():
9696
{'role': 'user', 'content': 'Solve 8x + 7 = -23'},
9797
],
9898
output_schema={
99-
'name': 'math_reasoning',
99+
'title': 'math_reasoning',
100100
'schema': {
101101
'type': 'object',
102102
'properties': {
@@ -136,15 +136,18 @@ async def agent_example():
136136

137137
# Define output schema
138138
math_schema = {
139-
'type': 'object',
140-
'properties': {
141-
'solution': {
142-
'type': 'string',
143-
'description': 'The step-by-step solution to the math problem',
139+
'title': 'math_tutor',
140+
'schema': {
141+
'type': 'object',
142+
'properties': {
143+
'solution': {
144+
'type': 'string',
145+
'description': 'The step-by-step solution to the math problem',
146+
},
147+
'answer': {'type': 'string', 'description': 'The final answer'},
144148
},
145-
'answer': {'type': 'string', 'description': 'The final answer'},
149+
'required': ['solution', 'answer'],
146150
},
147-
'required': ['solution', 'answer'],
148151
}
149152

150153
# Create OpenAI agent

flo_ai/examples/vllm_agent_usage.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
vllm_base_url = os.getenv('VLLM_BASE_URL')
1212

13+
vllm_model = 'microsoft/phi-4'
14+
1315

1416
async def example_simple_vllm_agent():
1517
# Create a simple conversational agent with vLLM
@@ -19,7 +21,7 @@ async def example_simple_vllm_agent():
1921
.with_prompt('You are a helpful math tutor.')
2022
.with_llm(
2123
OpenAIVLLM(
22-
model='microsoft/phi-4',
24+
model=vllm_model,
2325
base_url=vllm_base_url,
2426
temperature=0.7,
2527
api_key='',
@@ -68,7 +70,7 @@ async def calculate(operation: str, x: float, y: float) -> float:
6870
)
6971
.with_llm(
7072
OpenAIVLLM(
71-
model='microsoft/phi-4',
73+
model=vllm_model,
7274
base_url=vllm_base_url,
7375
temperature=0.7,
7476
api_key='',
@@ -89,7 +91,7 @@ async def calculate(operation: str, x: float, y: float) -> float:
8991
async def example_vllm_structured_output():
9092
# Define output schema for structured responses with name field
9193
math_schema = {
92-
'name': 'math_solution',
94+
'title': 'math_solution',
9395
'schema': {
9496
'type': 'object',
9597
'properties': {
@@ -122,7 +124,7 @@ async def example_vllm_structured_output():
122124
)
123125
.with_llm(
124126
OpenAIVLLM(
125-
model='microsoft/phi-4',
127+
model=vllm_model,
126128
base_url=vllm_base_url,
127129
temperature=0.3,
128130
api_key='',
@@ -165,7 +167,7 @@ async def calculate(operation: str, x: float, y: float) -> float:
165167

166168
# Define structured output schema for calculation results
167169
calculation_report_schema = {
168-
'name': 'calculation_report',
170+
'title': 'calculation_report',
169171
'schema': {
170172
'type': 'object',
171173
'properties': {
@@ -217,7 +219,7 @@ async def calculate(operation: str, x: float, y: float) -> float:
217219
)
218220
.with_llm(
219221
OpenAIVLLM(
220-
model='microsoft/phi-4',
222+
model=vllm_model,
221223
base_url=vllm_base_url,
222224
temperature=0.3,
223225
api_key='',

flo_ai/flo_ai/llm/openai_llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ async def generate(
2828
kwargs['response_format'] = {'type': 'json_object'}
2929
kwargs['functions'] = [
3030
{
31-
'name': output_schema.get('name', 'default'),
31+
'name': output_schema.get('title', 'default'),
3232
'parameters': output_schema.get('schema', output_schema),
3333
}
3434
]
35-
kwargs['function_call'] = {'name': output_schema.get('name', 'default')}
35+
kwargs['function_call'] = {'name': output_schema.get('title', 'default')}
3636

3737
# Add JSON format instruction to the system prompt
3838
if messages and messages[0]['role'] == 'system':

flo_ai/flo_ai/llm/openai_vllm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ async def generate(
2525
) -> Any:
2626
# Convert output_schema to OpenAI format if provided
2727
if output_schema:
28-
kwargs['extra_body'] = {'guided_json': output_schema.get('schema')}
28+
kwargs['response_format'] = {
29+
'type': 'json_schema',
30+
'json_schema': {
31+
'name': output_schema.get('title', 'default'),
32+
'schema': output_schema.get('schema', output_schema),
33+
},
34+
}
2935

3036
# Add JSON format instruction to the system prompt
3137
if messages and messages[0]['role'] == 'system':

0 commit comments

Comments
 (0)