forked from sahil-sagwekar2652/semantic-search-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_embds.py
59 lines (46 loc) · 2.01 KB
/
get_embds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from langchain.embeddings.base import Embeddings
from typing import List
import requests
class LocalLlamaEmbeddings(Embeddings):
def __init__(self, url: str, headers: dict):
self.url = url
self.headers = headers
super().__init__()
def api_query(self, url: str, headers: dict, json: dict):
response = requests.post(url=self.url, headers=self.headers, json=json)
response = response.json()
return response["data"][0]["embedding"]
def embed_documents(self, texts: List[str]) -> List[List[float]]:
# embeddings = []
async def post_multiple():
async with aiohttp.ClientSession() as session:
tasks = []
for text in texts: # replace with your range
url = self.url # replace with your API endpoint
payload = {"input": text} # replace with your payload
async with session.post(url, json=payload) as resp:
data = await resp.json()
tasks.append(data)
responses = await asyncio.gather(*tasks)
embeddings = asyncio.run(post_multiple())
# for text in texts:
# data = {
# "input": text,
# }
# embeddings.append(
# self.api_query(url=self.url, headers=self.headers, json=data)
# )
return embeddings
def embed_query(self, text: str) -> List[float]:
return self.api_query(url=self.url, headers=self.headers, json={"input": text})
async def post_multiple():
async with aiohttp.ClientSession() as session:
tasks = []
for number in range(1, 151): # replace with your range
url = f'https://your-api-endpoint/{number}' # replace with your API endpoint
payload = {"key": "value"} # replace with your payload
async with session.post(url, json=payload) as resp:
data = await resp.json()
tasks.append(data)
responses = await asyncio.gather(*tasks)
# process responses here