server-embd.py 939 B

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