1
0

server_embd.py 971 B

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