添加API密钥管理、模型服务和安全服务,并优化FastAPI应用程序配置

This commit is contained in:
yinpeng
2024-12-15 11:08:35 +08:00
parent 03c201c849
commit c56bea0b25
15 changed files with 638 additions and 438 deletions
+22
View File
@@ -0,0 +1,22 @@
import logging
import openai
from typing import Union, List, Dict, Any
logger = logging.getLogger(__name__)
class EmbeddingService:
def __init__(self, base_url: str):
self.base_url = base_url
async def create_embedding(
self, input_text: Union[str, List[str]], model: str, api_key: str
) -> Dict[str, Any]:
"""Create embeddings using OpenAI API"""
try:
client = openai.OpenAI(api_key=api_key, base_url=self.base_url)
response = client.embeddings.create(input=input_text, model=model)
return response
except Exception as e:
logger.error(f"Error creating embedding: {str(e)}")
raise