Last active
July 25, 2023 15:12
-
-
Save csiebler/9b6f7ed1ac8c0ecd4364c0f59de4419f to your computer and use it in GitHub Desktop.
Short example for using LangChain with Azure OpenAI Service
This file contains hidden or 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
import os | |
import openai | |
from dotenv import load_dotenv | |
from langchain.llms import AzureOpenAI | |
from langchain.embeddings import OpenAIEmbeddings | |
# Load environment variables (set OPENAI_API_KEY, OPENAI_API_BASE, and OPENAI_API_VERSION in .env) | |
load_dotenv() | |
# Configure OpenAI API | |
openai.api_type = "azure" | |
openai.api_base = os.getenv('OPENAI_API_BASE') | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
openai.api_version = os.getenv('OPENAI_API_VERSION') | |
# Create a completion | |
llm = AzureOpenAI(deployment_name="text-davinci-003") | |
joke = llm("Tell me a dad joke") | |
print(joke) | |
# Create embeddings | |
embeddings = OpenAIEmbeddings(deployment_id="text-embedding-ada-002", chunk_size=1) | |
text = "This is a test document." | |
# Embed a single document | |
query_result = embeddings.embed_query(text) | |
print(query_result) | |
# Embed multiple documents at once | |
doc_result = embeddings.embed_documents([text, text]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment