Skip to content

Commit 106d9ce

Browse files
committed
added better defaults
1 parent 7f56b08 commit 106d9ce

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

osbot_llms/fast_api/routes/Routes__Chat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
ROUTES_PATHS__CONFIG = ['/config/status', '/config/version']
1717
HEADER_NAME__CHAT_ID = 'osbot-llms-chat-id'
1818
HEADER_NAME__CHAT_THREAD_ID = 'osbot-llms-thread-id'
19+
HEADER_NAME__CHAT_PLATFORM = 'osbot-llms-platform'
20+
HEADER_NAME__CHAT_PROVIDER = 'osbot-llms-provider'
21+
HEADER_NAME__CHAT_MODEL = 'osbot-llms-model'
1922

2023
class Routes__Chat(Fast_API_Routes):
2124
tag : str = 'chat'
@@ -78,6 +81,11 @@ async def completion(self, request: Request, llm_chat_completion: LLMs__Chat_Com
7881

7982
routes_open_ai = Routes__OpenAI()
8083
user_data = llm_chat_completion.user_data
84+
if user_data is None:
85+
user_data = dict(selected_platform = llm_chat_completion.llm_platform ,
86+
selected_provider = llm_chat_completion.llm_provider ,
87+
selected_model = llm_chat_completion.llm_model )
88+
llm_chat_completion.user_data = user_data
8189

8290
# for now use the code in routes_open_ai.prompt_with_system__stream which is already working for OpenAI
8391
if user_data and 'selected_platform' in user_data and user_data.get('selected_platform') != 'OpenAI (Paid)':
@@ -86,6 +94,9 @@ async def completion(self, request: Request, llm_chat_completion: LLMs__Chat_Com
8694
pass
8795
response.headers.append(HEADER_NAME__CHAT_ID , chat_save_result.get('public_chat_id' ,''))
8896
response.headers.append(HEADER_NAME__CHAT_THREAD_ID, chat_save_result.get('public_chat_thread__id',''))
97+
response.headers.append(HEADER_NAME__CHAT_PLATFORM , user_data.get('selected_platform' ,''))
98+
response.headers.append(HEADER_NAME__CHAT_PROVIDER , user_data.get('selected_provider' ,''))
99+
response.headers.append(HEADER_NAME__CHAT_MODEL , user_data.get('selected_model' ,''))
89100
return response
90101
else:
91102
stream = llm_chat_completion.stream

osbot_llms/models/LLMs__Chat_Completion.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
from osbot_llms.models.GPT_Prompt_With_System_And_History import GPT_Prompt_With_System_And_History
77

8-
SWAGGER_EXAMPLE__LLMs__Chat_Completion = Body(..., example=dict(user_prompt ='Good morning, what is 44-2?',
8+
SWAGGER_EXAMPLE__LLMs__Chat_Completion = Body(..., example=dict(user_prompt ='Good morning, what is 44-2?' ,
99
system_prompts = ['use emojis in the answer' ],
1010
#temperature = 0.0 ,
11-
seed = 42 ,
12-
stream = False ))
11+
seed = 42 ,
12+
stream = False ,
13+
llm_platform = "Groq (Free)" ,
14+
llm_provider = "1. Meta" ,
15+
llm_model = "llama-3.1-70b-versatile" ))
1316

1417
@dataclass
1518
class LLMs__Chat_Completion(GPT_Prompt_With_System_And_History):

osbot_llms/test__chained_fast_api.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from unittest import TestCase
2+
3+
import httpx
4+
from fastapi import FastAPI, Request
5+
from osbot_utils.helpers.Random_Guid import Random_Guid
6+
from osbot_utils.utils.Http import url_join_safe
7+
from osbot_fast_api.utils.Fast_API_Server import Fast_API_Server
8+
from starlette.responses import StreamingResponse
9+
10+
11+
class test__chained_fast_api(TestCase):
12+
13+
def test_check__fast_api_2(self):
14+
15+
# FastAPI_2 (Backend Service):
16+
app_2 = FastAPI()
17+
18+
async def generate_stream_data(api_key):
19+
for i in range(3):
20+
yield f"Data chunk {i}: {api_key}\n"
21+
22+
@app_2.post("/chat/completions")
23+
async def chat_completions(request: Request):
24+
request_body = await request.json()
25+
api_key = request_body.get("api_key", '(no key)')
26+
return StreamingResponse(generate_stream_data(api_key), media_type="text/plain")
27+
28+
29+
# FastAPI_1 (Intermediate Service)
30+
app_1 = FastAPI()
31+
32+
async def proxy_stream(url: str, json_payload: dict, headers: dict):
33+
async with httpx.AsyncClient() as client:
34+
async with client.stream("POST", url, json=json_payload, headers=headers) as response:
35+
async for chunk in response.aiter_text():
36+
yield chunk
37+
38+
39+
@app_1.post("/chat/completions")
40+
async def chat_completions(request: Request):
41+
request_body = await request.json()
42+
api_key = f"YOUR_API_KEY : {Random_Guid()}"
43+
backend_url = request_body.get("backend_url")
44+
request_body["api_key"] = api_key
45+
46+
response = StreamingResponse(proxy_stream(backend_url, request_body, headers={"accept": "text/plain"}),media_type="text/plain")
47+
response.headers['api_key'] = api_key
48+
return response
49+
50+
with Fast_API_Server(app=app_2) as fast_api_2:
51+
response_1 = fast_api_2.requests_post("/chat/completions", data={})
52+
assert response_1.status_code == 200
53+
assert response_1.text == 'Data chunk 0: (no key)\nData chunk 1: (no key)\nData chunk 2: (no key)\n'
54+
55+
post_data = dict(user_prompt = '40_2',
56+
backend_url = url_join_safe(fast_api_2.url(), '/chat/completions'))
57+
with Fast_API_Server(app=app_1) as fast_api_1:
58+
response_2 = fast_api_1.requests_post("/chat/completions", data=post_data)
59+
assert response_2.status_code == 200
60+
api_key = response_2.headers.get('api_key')
61+
assert 'YOUR_API_KEY' in api_key
62+
assert response_2.text == (f'Data chunk 0: {api_key}\n'
63+
f'Data chunk 1: {api_key}\n'
64+
f'Data chunk 2: {api_key}\n')

tests/integration/fast_api/routes/test__api__Routes__Chat.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from osbot_llms.OSBot_LLMs__Server_Config import osbot_llms__server_config
55
from osbot_llms.OSBot_LLMs__Shared_Objects import osbot_llms__shared_objects
66
from osbot_llms.backend.s3_minio.S3_DB__Chat_Threads import CHAT__REQUEST_TYPE__USER_RESPONSE
7-
from osbot_llms.fast_api.routes.Routes__Chat import HEADER_NAME__CHAT_THREAD_ID, HEADER_NAME__CHAT_ID
7+
from osbot_llms.fast_api.routes.Routes__Chat import HEADER_NAME__CHAT_THREAD_ID, HEADER_NAME__CHAT_ID, \
8+
HEADER_NAME__CHAT_PLATFORM, HEADER_NAME__CHAT_PROVIDER, HEADER_NAME__CHAT_MODEL
89
from osbot_llms.models.LLMs__Chat_Completion import LLMs__Chat_Completion
910
from osbot_llms.testing.TestCase__S3_Minio__Temp_Chat_Threads import TestCase__S3_Minio__Temp_Chat_Threads
1011
from tests.llm_fast_api__for_tests import llm_fast_api__client
@@ -36,7 +37,9 @@ def test__completion__save_chat_completion__user_request(self):
3637
s3_key = f'{s3_folder}{request_type}.json.gz'
3738
file_data = self.s3_db_chat_threads.s3_file_data(s3_key)
3839

39-
assert list_set(dict(response.headers)) == [ 'content-type', 'fast-api-request-id', HEADER_NAME__CHAT_ID, HEADER_NAME__CHAT_THREAD_ID]
40+
assert list_set(dict(response.headers)) == sorted([ 'content-type', 'fast-api-request-id',
41+
HEADER_NAME__CHAT_ID, HEADER_NAME__CHAT_THREAD_ID,
42+
HEADER_NAME__CHAT_PLATFORM, HEADER_NAME__CHAT_PROVIDER, HEADER_NAME__CHAT_MODEL])
4043
assert self.s3_db_chat_threads.s3_folder_files(s3_folder) == [ 'user-request.json.gz', 'user-response.json.gz']
4144
assert list_set(file_data) == [ 'chat_thread_id', 'histories', 'images', 'llm_answer',
4245
'llm_model', 'llm_platform', 'llm_provider', 'max_tokens',

tests/integration/fast_api/test_Fast_API__via_Http.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ def test_version(self):
3838

3939
def test__chat__completion(self):
4040
with self.llm_fast_api as _:
41-
llm_chat_completion = LLMs__Chat_Completion(user_prompt='51-9')
41+
kwargs = dict(llm_platform = "Groq (Free)" ,
42+
llm_provider = "1. Meta" ,
43+
llm_model = "llama-3.1-70b-versatile" ,
44+
user_prompt = '51-9' )
45+
llm_chat_completion = LLMs__Chat_Completion(**kwargs)
4246
response = self.fast_api_server.requests_post('/chat/completion', data=llm_chat_completion)
47+
#pprint(dict(response.headers))
48+
#pprint(response.text)
4349
assert '42' in response.text
4450

4551

0 commit comments

Comments
 (0)