echodict/llama.cpp
version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786
0773
1import asyncio2import asyncio.threads3import requests4import numpy as np5 6 7n = 88 9result = []10 11async def requests_post_async(*args, **kwargs):12 return await asyncio.threads.to_thread(requests.post, *args, **kwargs)13 14async def main():15 model_url = "http://127.0.0.1:6900"16 responses: list[requests.Response] = await asyncio.gather(*[requests_post_async(17 url= f"{model_url}/embedding",18 json= {"content": "a "*1022}19 ) for i in range(n)])20 21 for response in responses:22 embedding = response.json()["embedding"]23 print(embedding[-8:])24 result.append(embedding)25 26asyncio.run(main())27 28# compute cosine similarity29 30for i in range(n-1):31 for j in range(i+1, n):32 embedding1 = np.array(result[i])33 embedding2 = np.array(result[j])34 similarity = np.dot(embedding1, embedding2) / (np.linalg.norm(embedding1) * np.linalg.norm(embedding2))35 print(f"Similarity between {i} and {j}: {similarity:.2f}")36 