1- import uuid
2- from fastapi import FastAPI , Request
3- from pydantic import BaseModel
4- import os
51import traceback
6- from dotenv import load_dotenv
2+ import uuid
73
8- load_dotenv () # take environment variables from .env file
4+ from crewai import Agent , Crew , Process , Task
5+ from dotenv import load_dotenv
6+ from fastapi import FastAPI
7+ from pydantic import BaseModel
98
10- # --- CrewAI imports ---
11- from crewai import Agent , Task , Crew , Process
9+ load_dotenv ()
1210
1311app = FastAPI ()
1412
1513@app .get ("/health" )
1614def health ():
1715 return {"ok" : True }
1816
17+
1918class ChatCompletionRequest (BaseModel ):
2019 model : str | None = None
2120 messages : list [dict ] # [{"role":"user","content":"hello"}]
2221 temperature : float | None = None
2322 stream : bool | None = False
2423
24+
2525def run_crewai (user_text : str ) -> str :
2626 # Minimal CrewAI example: 1 agent, 1 task
2727 agent = Agent (
@@ -33,9 +33,9 @@ def run_crewai(user_text: str) -> str:
3333 )
3434
3535 task = Task (
36- description = f"User said: { user_text } \n Reply to the user." ,
37- expected_output = "A helpful natural-language reply to the user." ,
38- agent = agent ,
36+ description = f"User said: { user_text } \n Reply to the user." ,
37+ expected_output = "A helpful natural-language reply to the user." ,
38+ agent = agent ,
3939 )
4040
4141 crew = Crew (
@@ -48,6 +48,7 @@ def run_crewai(user_text: str) -> str:
4848 result = crew .kickoff ()
4949 return str (result )
5050
51+
5152@app .post ("/v1/chat/completions" )
5253async def chat_completions (req : ChatCompletionRequest ):
5354 try :
@@ -62,7 +63,13 @@ async def chat_completions(req: ChatCompletionRequest):
6263 return {
6364 "id" : f"chatcmpl-{ uuid .uuid4 ().hex } " ,
6465 "object" : "chat.completion" ,
65- "choices" : [{"index" : 0 , "message" : {"role" : "assistant" , "content" : answer }, "finish_reason" : "stop" }],
66+ "choices" : [
67+ {
68+ "index" : 0 ,
69+ "message" : {"role" : "assistant" , "content" : answer },
70+ "finish_reason" : "stop" ,
71+ }
72+ ],
6673 }
6774 except Exception as e :
6875 traceback .print_exc ()
@@ -71,4 +78,4 @@ async def chat_completions(req: ChatCompletionRequest):
7178 "message" : str (e ),
7279 "type" : "internal_error" ,
7380 }
74- }
81+ }
0 commit comments