Update apis/chat_api.py
Browse files- apis/chat_api.py +4 -2
apis/chat_api.py
CHANGED
|
@@ -116,7 +116,7 @@ class ChatAPIApp:
|
|
| 116 |
model_name: str = Field(..., example="bert-base-uncased")
|
| 117 |
api_key: str = Field(..., example="your_hf_api_key_here")
|
| 118 |
|
| 119 |
-
async def
|
| 120 |
api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_name}"
|
| 121 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 122 |
response = requests.post(api_url, headers=headers, json={"inputs": texts})
|
|
@@ -186,11 +186,13 @@ class ChatAPIApp:
|
|
| 186 |
data_response = streamer.chat_return_dict(stream_response)
|
| 187 |
return data_response
|
| 188 |
|
|
|
|
|
|
|
| 189 |
async def embedding(request: QueryRequest):
|
| 190 |
try:
|
| 191 |
for attempt in range(3): # Retry logic
|
| 192 |
try:
|
| 193 |
-
embeddings = await
|
| 194 |
data = [
|
| 195 |
{"object": "embedding", "index": i, "embedding": embedding}
|
| 196 |
for i, embedding in enumerate(embeddings)
|
|
|
|
| 116 |
model_name: str = Field(..., example="bert-base-uncased")
|
| 117 |
api_key: str = Field(..., example="your_hf_api_key_here")
|
| 118 |
|
| 119 |
+
async def chat_embedding(texts, model_name, api_key):
|
| 120 |
api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_name}"
|
| 121 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 122 |
response = requests.post(api_url, headers=headers, json={"inputs": texts})
|
|
|
|
| 186 |
data_response = streamer.chat_return_dict(stream_response)
|
| 187 |
return data_response
|
| 188 |
|
| 189 |
+
|
| 190 |
+
|
| 191 |
async def embedding(request: QueryRequest):
|
| 192 |
try:
|
| 193 |
for attempt in range(3): # Retry logic
|
| 194 |
try:
|
| 195 |
+
embeddings = await chat_embedding(request.texts, request.model_name, request.api_key)
|
| 196 |
data = [
|
| 197 |
{"object": "embedding", "index": i, "embedding": embedding}
|
| 198 |
for i, embedding in enumerate(embeddings)
|