Niansuh commited on
Commit
11ee54e
·
verified ·
1 Parent(s): f0b2356

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +53 -29
api/utils.py CHANGED
@@ -59,18 +59,20 @@ class Editee:
59
  try:
60
  async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
61
  response.raise_for_status()
62
- if response.content_type == 'text/event-stream':
63
- async for line in response.content:
64
- yield line.decode('utf-8')
65
- else:
66
  response_data = await response.json()
67
  yield response_data['text']
 
 
 
 
68
  except Exception as e:
69
  logger.error(f"Error in Editee API call: {e}")
70
  raise HTTPException(status_code=500, detail="Error in Editee API call")
71
 
72
- # Function to process the response
73
- async def process_response(request: ChatRequest, stream: bool = False):
74
  try:
75
  model = MODEL_MAPPING.get(request.model, request.model)
76
  messages = [
@@ -84,30 +86,52 @@ async def process_response(request: ChatRequest, stream: bool = False):
84
  proxy=None # Add proxy if needed
85
  )
86
 
87
- if stream:
88
- async def event_generator():
89
- async for chunk in generator:
90
- yield f"data: {chunk}\n\n"
91
- return event_generator()
92
- else:
93
- full_response = ""
94
- async for chunk in generator:
95
- full_response += chunk
96
 
97
- return {
98
- "id": f"chatcmpl-{uuid.uuid4()}",
99
- "object": "chat.completion",
100
- "created": int(uuid.uuid1().time),
101
- "model": model,
102
- "choices": [
103
- {
104
- "index": 0,
105
- "message": {"role": "assistant", "content": full_response},
106
- "finish_reason": "stop",
107
- }
108
- ],
109
- "usage": None,
110
- }
111
  except Exception as e:
112
  logger.error(f"Error processing response: {e}")
113
  raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  try:
60
  async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
61
  response.raise_for_status()
62
+ # Check if the response is in JSON format
63
+ if response.headers.get('Content-Type') == 'application/json':
 
 
64
  response_data = await response.json()
65
  yield response_data['text']
66
+ else:
67
+ # Stream the response line by line
68
+ async for line in response.content:
69
+ yield line.decode('utf-8')
70
  except Exception as e:
71
  logger.error(f"Error in Editee API call: {e}")
72
  raise HTTPException(status_code=500, detail="Error in Editee API call")
73
 
74
+ # Function to process non-streaming response
75
+ async def process_response(request: ChatRequest):
76
  try:
77
  model = MODEL_MAPPING.get(request.model, request.model)
78
  messages = [
 
86
  proxy=None # Add proxy if needed
87
  )
88
 
89
+ full_response = ""
90
+ async for chunk in generator:
91
+ full_response += chunk
 
 
 
 
 
 
92
 
93
+ return {
94
+ "id": f"chatcmpl-{uuid.uuid4()}",
95
+ "object": "chat.completion",
96
+ "created": int(uuid.uuid1().time),
97
+ "model": model,
98
+ "choices": [
99
+ {
100
+ "index": 0,
101
+ "message": {"role": "assistant", "content": full_response},
102
+ "finish_reason": "stop",
103
+ }
104
+ ],
105
+ "usage": None,
106
+ }
107
  except Exception as e:
108
  logger.error(f"Error processing response: {e}")
109
  raise HTTPException(status_code=500, detail=str(e))
110
+
111
+ # Function to process streaming response
112
+ def process_response_stream(request: ChatRequest):
113
+ try:
114
+ model = MODEL_MAPPING.get(request.model, request.model)
115
+ messages = [
116
+ {"role": message.role, "content": message.content}
117
+ for message in request.messages
118
+ ]
119
+
120
+ generator = Editee.create_async_generator(
121
+ model=model,
122
+ messages=messages,
123
+ proxy=None # Add proxy if needed
124
+ )
125
+
126
+ async def event_generator():
127
+ try:
128
+ async for chunk in generator:
129
+ yield f"data: {chunk}\n\n"
130
+ yield "data: [DONE]\n\n"
131
+ except Exception as e:
132
+ logger.error(f"Error in streaming response: {e}")
133
+ raise HTTPException(status_code=500, detail=str(e))
134
+ return event_generator()
135
+ except Exception as e:
136
+ logger.error(f"Error processing streaming response: {e}")
137
+ raise HTTPException(status_code=500, detail=str(e))