-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,62 @@ | ||
""""Fetch embeddings from the Microsoft Azure OpenAI API""" | ||
|
||
import os | ||
import openai | ||
|
||
import tiktoken | ||
from dotenv import load_dotenv | ||
from openai import AzureOpenAI | ||
|
||
import dotenv | ||
dotenv.load_dotenv() | ||
load_dotenv() | ||
|
||
# https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/embeddings?tabs=python | ||
|
||
|
||
def safe_get(key): | ||
value = os.environ.get(key) | ||
if not value: | ||
raise Exception(f"Environment variable {key} not defined") | ||
return value | ||
|
||
|
||
OPENAI_API_KEY = safe_get("OPENAI_API_KEY") | ||
OPENAI_ENDPOINT = safe_get("OPENAI_ENDPOINT") | ||
|
||
openai.api_type = "azure" | ||
openai.api_key = OPENAI_API_KEY | ||
openai.api_base = OPENAI_ENDPOINT | ||
openai.api_version = "2023-05-15" # be sure it's the good one | ||
|
||
client = AzureOpenAI( | ||
api_key=OPENAI_API_KEY, azure_endpoint=OPENAI_ENDPOINT, api_version="2023-05-15" | ||
) | ||
enc = tiktoken.get_encoding("cl100k_base") | ||
|
||
|
||
def fetch_embedding(tokens): | ||
"""Fetch embedding for a list of tokens from the Microsoft Azure OpenAI API""" | ||
OPENAI_API_ENGINE = safe_get("OPENAI_API_ENGINE") | ||
|
||
response = openai.Embedding.create( | ||
input=tokens, | ||
engine=OPENAI_API_ENGINE | ||
) | ||
embeddings = response['data'][0]['embedding'] | ||
|
||
response = client.embeddings.create(input=tokens, engine=OPENAI_API_ENGINE) | ||
embeddings = response["data"][0]["embedding"] | ||
return embeddings | ||
|
||
|
||
# def fetch_tokens_embeddings(text): | ||
# tokens = get_tokens_from_text(text) | ||
# embeddings = fetch_embedding(tokens) | ||
# return (tokens, embeddings) | ||
|
||
|
||
def get_tokens_from_text(text): | ||
tokens = enc.encode(text) | ||
return tokens | ||
|
||
|
||
def get_chat_answer(system_prompt, user_prompt, max_token): | ||
OPENAI_API_ENGINE = safe_get("OPENAI_API_ENGINE") | ||
|
||
response = openai.ChatCompletion.create( | ||
response = client.chat.completions.create( | ||
engine=OPENAI_API_ENGINE, | ||
temperature=0, | ||
max_tokens=max_token, | ||
messages = [ | ||
messages=[ | ||
{"role": "system", "content": system_prompt}, | ||
{"role": "user", "content": (user_prompt)} | ||
] | ||
{"role": "user", "content": (user_prompt)}, | ||
], | ||
) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
|
||
import openai | ||
import os | ||
import os | ||
import sys | ||
import dotenv | ||
dotenv.load_dotenv() | ||
|
||
openai.api_type = "azure" | ||
openai.api_key = os.environ["OPENAI_API_KEY"] | ||
openai.api_base = f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com" | ||
openai.api_version = "2023-05-15" | ||
from dotenv import load_dotenv | ||
from openai import AzureOpenAI | ||
|
||
load_dotenv() | ||
|
||
|
||
client = AzureOpenAI( | ||
api_key=os.environ["OPENAI_API_KEY"], | ||
azure_endpoint=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com", | ||
api_version="2023-05-15", | ||
) | ||
|
||
|
||
def fetch_embedding(text): | ||
"""Fetch embedding for a list of tokens from the Microsoft Azure OpenAI API""" | ||
response = openai.Embedding.create( | ||
input=text, | ||
engine="ada" | ||
) | ||
embeddings = response['data'][0]['embedding'] | ||
response = client.embeddings.create(input=text, model="ada") | ||
embeddings = response.data[0].embedding | ||
return embeddings | ||
|
||
if __name__ == '__main__': | ||
|
||
if __name__ == "__main__": | ||
text = " ".join(sys.argv[1:]) | ||
if len(text) == 0: | ||
print('Please provide a text to embed') | ||
print("Please provide a text to embed") | ||
raise SystemExit | ||
print(fetch_embedding(text)) | ||
print(fetch_embedding(text)) |