/ examples / server-embd.py
server-embd.py
 1  import asyncio
 2  import requests
 3  import numpy as np
 4  
 5  n = 8
 6  
 7  result = []
 8  
 9  async def requests_post_async(*args, **kwargs):
10      return await asyncio.to_thread(requests.post, *args, **kwargs)
11  
12  async def main():
13      model_url = "http://127.0.0.1:6900"
14      responses: list[requests.Response] = await asyncio.gather(*[requests_post_async(
15          url= f"{model_url}/embedding",
16          json= {"content": str(0)*1024}
17      ) for i in range(n)])
18  
19      for response in responses:
20          embedding = response.json()["embedding"]
21          print(embedding[-8:])
22          result.append(embedding)
23  
24  asyncio.run(main())
25  
26  # compute cosine similarity
27  
28  for i in range(n-1):
29      for j in range(i+1, n):
30          embedding1 = np.array(result[i])
31          embedding2 = np.array(result[j])
32          similarity = np.dot(embedding1, embedding2) / (np.linalg.norm(embedding1) * np.linalg.norm(embedding2))
33          print(f"Similarity between {i} and {j}: {similarity:.2f}")
34