@@ -152,18 +152,20 @@ AG-UI events:
152152from fastapi import FastAPI, Request
153153from fastapi.responses import StreamingResponse
154154from ag_ui.core import (
155- RunAgentInput,
156- Message,
157- EventType,
158- RunStartedEvent,
159- RunFinishedEvent,
160- TextMessageStartEvent,
161- TextMessageContentEvent,
162- TextMessageEndEvent
155+ RunAgentInput,
156+ Message,
157+ EventType,
158+ RunStartedEvent,
159+ RunFinishedEvent,
160+ TextMessageStartEvent,
161+ TextMessageContentEvent,
162+ TextMessageEndEvent
163163)
164164from ag_ui.encoder import EventEncoder
165165import uuid
166166from openai import OpenAI
167+ import dotenv
168+ import os
167169
168170app = FastAPI(title = " AG-UI Endpoint" )
169171
@@ -175,18 +177,27 @@ async def my_endpoint(input_data: RunAgentInput):
175177
176178 # Send run started event
177179 yield encoder.encode(
178- RunStartedEvent(
179- type = EventType.RUN_STARTED ,
180- thread_id = input_data.thread_id,
181- run_id = input_data.run_id
182- )
180+ RunStartedEvent(
181+ type = EventType.RUN_STARTED ,
182+ thread_id = input_data.thread_id,
183+ run_id = input_data.run_id
184+ )
183185 )
184186
185187 # Initialize OpenAI client
186- client = OpenAI()
188+ client = OpenAI(api_key = os.getenv(" OPENAI_API_KEY" ))
189+
190+ # Convert AG-UI messages to OpenAI messages format
191+ openai_messages = []
192+ for msg in input_data.messages:
193+ if msg.role in [" user" , " system" , " assistant" ]:
194+ openai_messages.append({
195+ " role" : msg.role,
196+ " content" : msg.content or " "
197+ })
187198
188199 # Generate a message ID for the assistant's response
189- message_id = uuid.uuid4()
200+ message_id = str ( uuid.uuid4() )
190201
191202 # Send text message start event
192203 yield encoder.encode(
@@ -206,7 +217,12 @@ async def my_endpoint(input_data: RunAgentInput):
206217
207218 # Process the streaming response and send content events
208219 for chunk in stream:
209- if hasattr (chunk.choices[0 ].delta, " content" ) and chunk.choices[0 ].delta.content:
220+ if (chunk.choices and
221+ len (chunk.choices) > 0 and
222+ chunk.choices[0 ].delta and
223+ hasattr (chunk.choices[0 ].delta, ' content' ) and
224+ chunk.choices[0 ].delta.content):
225+
210226 content = chunk.choices[0 ].delta.content
211227 yield encoder.encode(
212228 TextMessageContentEvent(
@@ -226,11 +242,11 @@ async def my_endpoint(input_data: RunAgentInput):
226242
227243 # Send run finished event
228244 yield encoder.encode(
229- RunFinishedEvent(
230- type = EventType.RUN_FINISHED ,
231- thread_id = input_data.thread_id,
232- run_id = input_data.run_id
233- )
245+ RunFinishedEvent(
246+ type = EventType.RUN_FINISHED ,
247+ thread_id = input_data.thread_id,
248+ run_id = input_data.run_id
249+ )
234250 )
235251
236252 return StreamingResponse(
@@ -251,6 +267,28 @@ export OPENAI_API_KEY=your-api-key
251267poetry run uvicorn my_endpoint.main:app --reload
252268```
253269
270+ Test your endpoint with:
271+ ``` bash
272+ curl -X POST http://localhost:8000/awp \
273+ -H " Content-Type: application/json" \
274+ -H " Accept: text/event-stream" \
275+ -d ' {
276+ "threadId": "thread_123",
277+ "runId": "run_456",
278+ "state": {},
279+ "messages": [
280+ {
281+ "id": "msg_1",
282+ "role": "user",
283+ "content": "Hello, how are you?"
284+ }
285+ ],
286+ "tools": [],
287+ "context": [],
288+ "forwardedProps": {}
289+ }'
290+ ```
291+
254292This implementation creates a fully functional AG-UI endpoint that processes
255293messages and streams back the responses in real-time.
256294
0 commit comments