Skip to content

Commit de3f0bd

Browse files
committed
added llm-api-key header
1 parent 08425a7 commit de3f0bd

File tree

8 files changed

+51
-18
lines changed

8 files changed

+51
-18
lines changed

osbot_llms/OSBot_LLMs__Shared_Objects.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def s3_db_chat_threads(self): # to
1212
kwargs = dict(use_minio = use_minio ,
1313
server_name = server_name)
1414
with S3_DB__Chat_Threads(**kwargs) as _:
15+
_.bucket_name__insert_account_id = use_minio is False
1516
_.setup() # set up tasks, including creating target bucket if it doesn't exist
1617
_.s3_key_generator.use_request_path = False
1718
_.s3_key_generator.use_when = True

osbot_llms/backend/s3_minio/S3_DB_Base.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ def setup(self):
178178

179179
if self.s3().bucket_not_exists(bucket_name):
180180
result = self.s3().bucket_create(**kwargs)
181-
# print("========= S3_DB_Base setup=======")
182-
# pprint(kwargs)
183-
# pprint(self.json())
184-
# pprint(result)
185-
# print("========= S3_DB_Base setup=======")
186181
if result.get('status') != 'ok':
187182
message = f"""Catastrophic error: Failed to create bucket:
188183

osbot_llms/fast_api/Fast_API__LLMs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66

77
class Fast_API__LLMs(Fast_API):
8-
base_path : str = '/llms'
8+
base_path : str = '/llms'
9+
enable_cors: bool = True
910

1011
def setup_routes(self):
1112
self.add_routes(Routes__Chat)
1213
self.add_routes(Routes__Info)
14+
15+

osbot_llms/fast_api/routes/Routes__Chat.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from fastapi import Request
55
from fastapi.params import Header, Body
6+
from osbot_utils.helpers.Random_Guid import Random_Guid
67
from osbot_utils.utils.Dev import pprint
78
from starlette.responses import StreamingResponse
89
from osbot_fast_api.api.Fast_API_Routes import Fast_API_Routes
@@ -75,7 +76,7 @@ async def simulated_api_call(): # Simulating the respons
7576
traceback.print_exc()
7677

7778

78-
async def completion(self, request: Request, llm_chat_completion: LLMs__Chat_Completion = SWAGGER_EXAMPLE__LLMs__Chat_Completion):
79+
async def completion(self, request: Request, llm_chat_completion: LLMs__Chat_Completion = SWAGGER_EXAMPLE__LLMs__Chat_Completion, llm_api_key:str = Header(default="xxxx.aaaa.bbbb.xxxx")):
7980
request_id = self.request_id(request)
8081
chat_save_result = self.chats_storage_s3_minio.save_user_request(llm_chat_completion, request_id)
8182

@@ -91,12 +92,11 @@ async def completion(self, request: Request, llm_chat_completion: LLMs__Chat_Com
9192
if user_data and 'selected_platform' in user_data and user_data.get('selected_platform') != 'OpenAI (Paid)':
9293
response = await self.handle_other_llms(llm_chat_completion, request, request_id)
9394
if type(response) == StreamingResponse:
94-
pass
95-
response.headers.append(HEADER_NAME__CHAT_ID , chat_save_result.get('public_chat_id' ,''))
96-
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' ,''))
95+
response.headers.append(HEADER_NAME__CHAT_ID , chat_save_result.get('public_chat_id' ) or '')
96+
response.headers.append(HEADER_NAME__CHAT_THREAD_ID, chat_save_result.get('public_chat_thread__id') or '')
97+
response.headers.append(HEADER_NAME__CHAT_PLATFORM , user_data.get('selected_platform' ) or '')
98+
response.headers.append(HEADER_NAME__CHAT_PROVIDER , user_data.get('selected_provider' ) or '')
99+
response.headers.append(HEADER_NAME__CHAT_MODEL , user_data.get('selected_model' ) or '')
100100
return response
101101
else:
102102
stream = llm_chat_completion.stream

tests/integration/backend/s3_minio/test_S3_DB__Chat_Threads.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def test_init(self):
3737
def test__check_setup(self):
3838
with self.s3_db_chat_threads as _:
3939
assert _.bucket_exists() is True
40-
assert _.s3_bucket() == f'{S3_DB_BASE__BUCKET_NAME__PREFIX}-{aws_config.account_id()}-chat-threads'
41-
assert _.json() == { 'bucket_name__insert_account_id': True,
42-
'bucket_name__prefix' : S3_DB_BASE__BUCKET_NAME__PREFIX,
43-
'bucket_name__suffix' : S3_BUCKET_SUFFIX__CHAT_THREADS ,
40+
assert _.s3_bucket() == f'{S3_DB_BASE__BUCKET_NAME__PREFIX}-chat-threads'
41+
assert _.json() == { 'bucket_name__insert_account_id': False ,
42+
'bucket_name__prefix' : S3_DB_BASE__BUCKET_NAME__PREFIX ,
43+
'bucket_name__suffix' : S3_BUCKET_SUFFIX__CHAT_THREADS ,
4444
's3_key_generator' : {'root_folder' : 'chat-threads',
4545
's3_path_block_size': 5,
4646
'save_as_gz' : True,

tests/integration/testing/test_TestCase__S3_Minio__Temp_Chat_Threads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test__setUpClass(self):
3131
assert type(self.s3_db_chat_threads) is S3_DB__Chat_Threads
3232
assert self.s3_db_chat_threads.bucket_exists() is True
3333
assert aws_config.account_id() == self.random_aws_creds.env_vars['AWS_ACCOUNT_ID']
34-
assert self.s3_db_chat_threads.s3_bucket() == f'{S3_DB_BASE__BUCKET_NAME__PREFIX}-{aws_config.account_id()}-chat-threads'
34+
assert self.s3_db_chat_threads.s3_bucket() == f'{S3_DB_BASE__BUCKET_NAME__PREFIX}-chat-threads'
3535
assert self.s3_db_chat_threads.use_minio is True
3636
assert self.s3_db_chat_threads.server_name == 'osbot-llms'
3737

tests/qa/test_Live_Server.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from unittest import TestCase
2+
3+
import pytest
4+
import requests
5+
from osbot_utils.utils.Dev import pprint
6+
from osbot_utils.utils.Env import get_env
7+
from osbot_utils.utils.Objects import dict_to_obj, base_types
8+
9+
from osbot_llms.models.LLMs__Chat_Completion import LLMs__Chat_Completion
10+
11+
ENV_NAME__URL_LIVE_SERVER = 'URL_LIVE_SERVER'
12+
DEFAULT_URL_LIVE_SERVER = 'http://localhost:5010'
13+
14+
@pytest.mark.skip("Wire up to CI pipline")
15+
class test_Live_Server(TestCase):
16+
17+
@classmethod
18+
def setUpClass(cls):
19+
cls.live_server = get_env(ENV_NAME__URL_LIVE_SERVER, DEFAULT_URL_LIVE_SERVER)
20+
21+
def test_server_connection(self):
22+
response = requests.get(self.live_server, allow_redirects=False)
23+
assert response.status_code == 307
24+
assert dict_to_obj(dict(response.headers)).location == '/docs'
25+
26+
def test_open_api(self):
27+
response = dict_to_obj(requests.get(self.live_server+'/openapi.json').json())
28+
assert response.info.title == 'FastAPI'
29+
assert getattr(response.paths, '/').get.summary == 'Redirect To Docs'
30+
31+
def test__chat__completion(self):
32+
llms_chat_completion = LLMs__Chat_Completion(stream=False)
33+
response = requests.post(self.live_server + '/chat/completion', json=llms_chat_completion.json())
34+
assert response.text == '"no engine"'

0 commit comments

Comments
 (0)