Skip to content

Commit

Permalink
new sample for embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastropy committed Feb 22, 2024
1 parent e6ac3d5 commit a72512e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,26 @@
"text": [
"512\n",
"# of embeddings for the first vector: 1\n",
"# of embeddings for the second vector: 4\n",
"# of embeddings for the second vector: 8\n",
"Results: {\n",
" \"result_list\": [\n",
" \"Black cat\",\n",
" \"Black cat\",\n",
" \"white Cat\",\n",
" \"white Cat\",\n",
" \"Red Cat\",\n",
" \"Red Cat\",\n",
" \"apple\",\n",
" \"apple\"\n",
" ],\n",
" \"scores_llist\": [\n",
" 0.9999999999999999,\n",
" 0.9999999999999999,\n",
" 0.7667772992427558,\n",
" 0.7667772992427558,\n",
" 0.7071684587870847,\n",
" 0.7071684587870847,\n",
" 0.2702694570913964,\n",
" 0.2702694570913964\n",
" ]\n",
"}\n"
Expand All @@ -548,7 +556,7 @@
"source": [
"from serverless_openai import OpenAIAPI, Similarity\n",
"from serverless_openai.helpers import cosine_similarity\n",
"import os\n",
"import os, json\n",
"\n",
"# Initialize OpenAIAPI with API Key\n",
"openai_api_key = os.getenv(\"openai_api_key\")\n",
Expand All @@ -563,15 +571,15 @@
"print(len(res1.result[0]))\n",
"\n",
"\n",
"prompt_list = [\"Black cat\", \"apple\", \"white Cat\", \"Red Cat\"]\n",
"prompt_list = [\"Black cat\", \"apple\", \"white Cat\", \"Red Cat\", \"Black cat\", \"apple\", \"white Cat\", \"Red Cat\"]\n",
"res2 = ai.embeddings(prompt=prompt_list, model=model, dimensions=512)\n",
"# matrix = [m[:512] for m in res2.result]\n",
"if res1.result and res2.result:\n",
" print(\"# of embeddings for the first vector:\", len(res1.result))\n",
" print(\"# of embeddings for the second vector:\",len(res2.result))\n",
"\n",
" sim = Similarity(vector=res1.result, matrix=res2.result)\n",
" result_list = cosine_similarity(sim, prompt_list)\n",
" result_list = cosine_similarity(sim, prompt_list, topn=10)\n",
" print(\"Results:\", json.dumps(result_list, indent=4))"
]
},
Expand Down

0 comments on commit a72512e

Please sign in to comment.