Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ygmpkk/dc627204c2f281718c125031b7f76af5 to your computer and use it in GitHub Desktop.
Save ygmpkk/dc627204c2f281718c125031b7f76af5 to your computer and use it in GitHub Desktop.
Using LlamaIndex (GPT Index) with Azure OpenAI Service
import os
import openai
from dotenv import load_dotenv
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, PromptHelper
from langchain.llms import AzureOpenAI
from langchain.embeddings import OpenAIEmbeddings
from llama_index import LangchainEmbedding
# Load env variables (create .env with OPENAI_API_KEY and OPENAI_API_BASE)
load_dotenv()
# Configure OpenAI API
openai.api_type = "azure"
openai.api_version = "2022-12-01"
openai.api_base = os.getenv('OPENAI_API_BASE')
openai.api_key = os.getenv("OPENAI_API_KEY")
deployment_name = "text-davinci-003"
# Create LLM via Azure OpenAI Service
llm = AzureOpenAI(deployment_name=deployment_name)
llm_predictor = LLMPredictor(llm=llm)
embedding_llm = LangchainEmbedding(OpenAIEmbeddings())
# Define prompt helper
max_input_size = 3000
num_output = 256
chunk_size_limit = 1000 # token window size per document
max_chunk_overlap = 20 # overlap for each token fragment
prompt_helper = PromptHelper(max_input_size=max_input_size, num_output=num_output, max_chunk_overlap=max_chunk_overlap, chunk_size_limit=chunk_size_limit)
# Read txt files from data directory
documents = SimpleDirectoryReader('data').load_data()
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, embed_model=embedding_llm, prompt_helper=prompt_helper)
index.save_to_disk("index.json")
# Query index with a question
response = index.query("What is azure openai service?")
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment