jonathanjordan21 commited on
Commit
844397e
1 Parent(s): 1ff3bbb

Update apis/chat_api.py

Browse files
Files changed (1) hide show
  1. apis/chat_api.py +25 -19
apis/chat_api.py CHANGED
@@ -29,26 +29,30 @@ from networks.openai_streamer import OpenaiStreamer
29
  from sentence_transformers import SentenceTransformer
30
  import tiktoken
31
 
32
- # class EmbeddingsAPIInference:
33
- # def __init__(self, model_name):
34
- # self.model_name=model_name
35
- # def encode(self, x, headers=None, api_key=None):
36
- # if api_key:
37
- # if headers:
38
- # headers.update({"Authorization": f"Bearer {api_key}"})
39
- # else:
40
- # headers = {"Authorization": f"Bearer {api_key}"}
41
 
42
- # API_URL = "https://api-inference.huggingface.co/models/"+self.model_name
43
 
44
- # payload = {
45
- # "inputs": {
46
- # "source_sentence": "That is a happy person",
47
- # "sentences": ["That is a happy dog", "That is a very happy person", "Today is a sunny day"],
48
- # }
49
- # }
 
 
 
 
 
 
50
 
51
- # requests.post(API_URL, headers=headers, json=payload).json()
52
 
53
 
54
 
@@ -65,6 +69,8 @@ class ChatAPIApp:
65
  "mxbai-embed-large":SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1"),
66
  "nomic-embed-text": SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True),
67
  "multilingual-e5-large-instruct":SentenceTransformer("intfloat/multilingual-e5-large-instruct"),
 
 
68
  }
69
 
70
  def get_available_models(self):
@@ -350,7 +356,7 @@ class ChatAPIApp:
350
  model = request.model
351
  model_kwargs = request.options
352
  encoding = tiktoken.get_encoding("cl100k_base")
353
- embeddings = self.embeddings[model].encode([encoding.decode(inp) for inp in request.input])#, **model_kwargs)
354
  return {
355
  "object":"list",
356
  "data":[
@@ -366,7 +372,7 @@ class ChatAPIApp:
366
  try:
367
  model = request.model
368
  model_kwargs = request.options
369
- embeddings = self.embeddings[model].encode(request.prompt)#, **model_kwargs)
370
  return {"embedding": embeddings.tolist()}
371
  except ValueError as e:
372
  raise HTTPException(status_code=400, detail=str(e))
 
29
  from sentence_transformers import SentenceTransformer
30
  import tiktoken
31
 
32
+ class EmbeddingsAPIInference:
33
+ def __init__(self, model_name):
34
+ self.model_name=model_name
35
+ def encode(self, x:str, api_key=None):
36
+ if api_key:
37
+ headers = {"Authorization": f"Bearer {api_key}"}
38
+ else:
39
+ headers = None
 
40
 
41
+ API_URL = "https://api-inference.huggingface.co/models/"+self.model_name
42
 
43
+ payload = {
44
+ "inputs": x,
45
+ "options":{"wait_for_model":True}
46
+ }
47
+
48
+ return requests.post(API_URL, headers=headers, json=payload).json()
49
+
50
+
51
+ class SentenceTransformer(SentenceTransformer):
52
+ def encode(self, **kwargs):
53
+ kwargs.pop("api_key", None)
54
+ return super().encode(**kwargs)
55
 
 
56
 
57
 
58
 
 
69
  "mxbai-embed-large":SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1"),
70
  "nomic-embed-text": SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True),
71
  "multilingual-e5-large-instruct":SentenceTransformer("intfloat/multilingual-e5-large-instruct"),
72
+ "intfloat/multilingual-e5-large-instruct":EmbeddingsAPIInference("intfloat/multilingual-e5-large-instruct"),
73
+ "mixedbread-ai/mxbai-embed-large-v1":EmbeddingsAPIInference("mixedbread-ai/mxbai-embed-large-v1")
74
  }
75
 
76
  def get_available_models(self):
 
356
  model = request.model
357
  model_kwargs = request.options
358
  encoding = tiktoken.get_encoding("cl100k_base")
359
+ embeddings = self.embeddings[model].encode([encoding.decode(inp) for inp in request.input], api_key=api_key)#, **model_kwargs)
360
  return {
361
  "object":"list",
362
  "data":[
 
372
  try:
373
  model = request.model
374
  model_kwargs = request.options
375
+ embeddings = self.embeddings[model].encode(request.prompt, api_key=api_key)#, **model_kwargs)
376
  return {"embedding": embeddings.tolist()}
377
  except ValueError as e:
378
  raise HTTPException(status_code=400, detail=str(e))