souljoy commited on
Commit
5a73c01
1 Parent(s): 65cafa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -1,12 +1,13 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import HTMLResponse
3
  from pydantic import BaseModel
4
- from fastapi.responses import JSONResponse
5
  import requests
6
  import json
7
  import openai
8
  import time
9
 
 
10
  class Text(BaseModel):
11
  content: str = ""
12
 
@@ -127,8 +128,7 @@ def chat_gpt_ep(content: Text = None):
127
  return JSONResponse(content=content)
128
 
129
 
130
- @app.post("/chatgptstream")
131
- def chat_gpt_stream_ep(content: Text = None):
132
  start_time = time.time()
133
  obj = json.loads(content.content)
134
  response = openai.ChatCompletion.create(
@@ -146,15 +146,16 @@ def chat_gpt_stream_ep(content: Text = None):
146
  chunk_message = chunk['choices'][0]['delta'] # extract the message
147
  collected_messages.append(chunk_message) # save the message
148
  print(f"Message received {chunk_time:.2f} seconds after request: {chunk_message}") # print the delay and text
 
 
 
 
 
149
 
150
- # print the time delay and text received
151
- print(f"Full response received {chunk_time:.2f} seconds after request")
152
- full_reply_content = ''.join([m.get('content', '') for m in collected_messages])
153
- print(f"Full conversation received: {full_reply_content}")
154
 
155
- content = {'content': full_reply_content}
156
- print('content:', content)
157
- return JSONResponse(content=content)
158
 
159
 
160
  @app.post("/embeddings")
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import HTMLResponse
3
  from pydantic import BaseModel
4
+ from fastapi.responses import JSONResponse, StreamingResponse
5
  import requests
6
  import json
7
  import openai
8
  import time
9
 
10
+
11
  class Text(BaseModel):
12
  content: str = ""
13
 
 
128
  return JSONResponse(content=content)
129
 
130
 
131
+ async def chat_gpt_stream_fun(content: Text = None):
 
132
  start_time = time.time()
133
  obj = json.loads(content.content)
134
  response = openai.ChatCompletion.create(
 
146
  chunk_message = chunk['choices'][0]['delta'] # extract the message
147
  collected_messages.append(chunk_message) # save the message
148
  print(f"Message received {chunk_time:.2f} seconds after request: {chunk_message}") # print the delay and text
149
+ full_reply_content = ''.join([m.get('content', '') for m in collected_messages])
150
+ print(f"Full conversation received: {full_reply_content}")
151
+ content = {'content': full_reply_content}
152
+ print('content:', content)
153
+ yield full_reply_content
154
 
 
 
 
 
155
 
156
+ @app.post("/chatgptstream")
157
+ async def get_random_numbers(content: Text = None):
158
+ return StreamingResponse(chat_gpt_stream_fun(content))
159
 
160
 
161
  @app.post("/embeddings")