-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (115 loc) · 4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from fastapi import Request, FastAPI
import json
import asyncio
from fastapi.responses import StreamingResponse
from sagemakerbot import SagemakerChatbot
import os
# sourcing AWS environment variables
AWS_ENDPOINT_NAME = os.environ["ENDPOINT_NAME_MISTRAL"]
AWS_REGION_NAME = os.environ["REGION_NAME"]
app = FastAPI()
def break_llm_output_string(response: str):
"""
Function to convert LLM response output string into smaller streamable chunks
"""
output = []
i = 0
while i + 6 < len(response):
chunk = response[i : i + 6]
output.append(chunk)
i += 6
if i < len(response):
output.append(response[i:])
return output
async def generate_llm_continuedev_stream(answer: str):
"""
Function takes a string as input, converts it into smaller chunks and streams it
"""
# convert the stream into streamable chunks
answer_output = break_llm_output_string(answer)
for i in range(len(answer_output)):
json_stream_chunk = {
"model": "mistral",
"created_at": "",
"response": f"{answer_output[i]}",
"done": False,
}
yield json.dumps(json_stream_chunk) + "\n"
await asyncio.sleep(
0.01
) # NOTE: Editable: 0.01 second delay between each streaming chunk
# sending the final chunk
json_stream_chunk = {
"model": "mistral",
"created_at": "",
"response": "",
"done": True,
"context": [],
"total_duration": "",
"load_duration": "",
"prompt_eval_count": "",
"prompt_eval_duration": "",
"eval_count": 0,
"eval_duration": 0,
}
yield json.dumps(json_stream_chunk) + "\n"
await asyncio.sleep(
0.01
) # NOTE: Editable: 0.01 second delay between each streaming chunk
async def generate_fake_llm_response():
"""
Function to stream a fake LLM response
"""
json_stream_chunk = {
"model": "mistral",
"created_at": "",
"response": "",
"done": True,
"context": [],
"total_duration": "",
"load_duration": "",
"prompt_eval_count": "",
"prompt_eval_duration": "",
"eval_count": 0,
"eval_duration": 0,
}
yield json.dumps(json_stream_chunk) + "\n"
await asyncio.sleep(
0.01
) # NOTE: Editable: 0.01 second delay between each streaming chunk
@app.post("/api/generate", response_class=StreamingResponse)
async def stream_continuedev_response(request: Request):
"""
Continuedev streaming endpoint service
"""
continue_dev_second_request_prompt = '[INST] ""\n\nPlease write a short title summarizing the message quoted above. Use no more than 10 words: [/INST]'
try:
# extracting chat prompt context
request_data = json.loads(await request.body())
user_prompt_question = (
request_data["template"].split("[INST]")[-1].split("[/INST]")[0].strip()
)
user_chat_context = ("[INST]").join(
request_data["template"].split("[INST]")[:-1]
)
# NOTE: capturing the continuedev second summarizing request prompt and overriding it
if request_data["template"] == continue_dev_second_request_prompt:
return StreamingResponse(
content=generate_fake_llm_response(), media_type="application/json"
)
# Initiating the sagemaker chain
sagemaker_bot = SagemakerChatbot(
endpoint_name=AWS_ENDPOINT_NAME, region_name=AWS_REGION_NAME
)
output = sagemaker_bot.chat(
chat_context=user_chat_context, question=user_prompt_question
)
# Streaming the llm response
return StreamingResponse(
content=generate_llm_continuedev_stream(output),
media_type="application/json",
)
except json.JSONDecodeError:
return {"error": "Invalid JSON format in the request body"}
except KeyError as e:
return {"error": f"Missing key: {e}"}