-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-embeddings.py
More file actions
51 lines (42 loc) · 1.42 KB
/
Copy path03-embeddings.py
File metadata and controls
51 lines (42 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# from openai import AzureOpenAI
# import os
# from dotenv import load_dotenv
# # Load environment variables from .env file
# load_dotenv()
# # Instantiate the Azure OpenAI client
# azure_open_ai_client = AzureOpenAI(
# api_version=os.environ["AZURE_OPENAI_API_VERSION"],
# base_url=os.environ["AZURE_OPENAI_ENDPOINT"],
# api_key=os.environ["OPENAI_API_KEY"]
# )
# # Create an embeddings
# embeddings = azure_open_ai_client.embeddings.create(
# model= os.environ["OPENAI_EMBEDDING_MODEL"],
# input=["hello world", "goodbye world"]
# )
# for i, e in enumerate(embeddings.data):
# print(f"Embedding {i}: {e.embedding[:5]}...") # Print first 5 values of each embedding
import os
from openai import AzureOpenAI
from dotenv import load_dotenv
load_dotenv()
endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
model_name =os.environ["OPENAI_EMBEDDING_MODEL"]
api_version = os.environ["AZURE_OPENAI_API_VERSION"]
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
api_key=os.environ["OPENAI_API_KEY"]
)
response = client.embeddings.create(
input=["first phrase","second phrase","third phrase"],
model=model_name
)
for item in response.data:
length = len(item.embedding)
print(
f"data[{item.index}]: length={length}, "
f"[{item.embedding[0]}, {item.embedding[1]}, "
f"..., {item.embedding[length-2]}, {item.embedding[length-1]}]"
)
print(response.usage)