Notebooks
P
Pinecone
00 Azure Openai Retrieval

00 Azure Openai Retrieval

vector-databasesemantic-searchlearnAILLMgenerationPythonjupyter-notebookpinecone-exampleslangchain

Open In Colab Open nbviewer

Using Azure's OpenAI with LangChain

[1]

Building the Knowledge Base

Adding an external knowledge to chatbots allows us to ground generation to this external knowledge. For our use-case our external knowledge will be the LangChain docs. We can load this from Pinecone datasets like so:

[2]

We must change the "url" field in the metadata column to "source" for compatibility with later LangChain components.

[3]

Our input docs are ready so we can move onto indexing everything.

Initializing the Index

Now we need a place to store these embeddings and enable a efficient vector search through them all. To do that we use Pinecone, we can get a free API key and enter it below where we will initialize our connection to Pinecone and create a new index.

[4]

Now we setup our index specification, this allows us to define the cloud provider and region where we want to deploy our index. You can find a list of all available providers and regions here.

[ ]

Create the index:

[5]
[6]
{'dimension': 1536,
, 'index_fullness': 0.0,
, 'namespaces': {},
, 'total_vector_count': 0}

Now we add all of our docs to Pinecone:

[7]
sending upsert requests:   0%|          | 0/6952 [00:00<?, ?it/s]
collecting async responses:   0%|          | 0/70 [00:00<?, ?it/s]
upserted_count: 6952

After indexing everything we can check the number of vectors in our index like so:

[8]
{'dimension': 1536,
, 'index_fullness': 0.0,
, 'namespaces': {'': {'vector_count': 3476}},
, 'total_vector_count': 3476}

Initializing Azure OpenAI

To use OpenAI's service via Azure we first need to setup the service in Azure and in Azure OpenAI Studio we need to create two Deployments, one using gpt-4 and another using text-embedding-ada-002.

Once we've done this we need to set a few environment variables (all found in Azure OpenAI Studio) like so:

[9]

We can now connect to both of our deployments via LangChain. First our ChatCompletion endpoint which uses gpt-3.5-turbo:

[10]

And then our embedding endpoint which uses text-embedding-ada-002:

[11]

Initializing Retrieval Component with LangChain

Before we move on, we must also initialize a connection to our index via LangChain. We need this for compatibility with later LangChain components. To use this we pass the index from above into a LangChain vectorstores.Pinecone object:

[12]

Initializing the RetrievalQA Component

The RetrievalQA and RetrievalQAWithSourcesChain are both components in LangChain that allow us to ask a natural language query and return a response grounded in the knowledge retrieved from our knowledge base. We can implement this and include original data sources like so:

[13]

Now we can begin asking questions about LangChain!

[14]
{'question': 'can you tell me about the PromptLayer for OpenAI in LangChain?',
, 'answer': 'PromptLayer for OpenAI in LangChain is a middleware that allows developers to track, manage, and share GPT prompt engineering. It records all OpenAI API requests, enabling users to search and explore request history in the PromptLayer dashboard. LangChain provides PromptLayer wrappers for LLM, PromptLayerChatOpenAI, and PromptLayerOpenAIChat. To use PromptLayer within LangChain, you need to install the promptlayer python library, create a PromptLayer account, and create an API token to set as an environment variable (PROMPTLAYER_API_KEY).\n\n',
, 'sources': '\n- https://python.langchain.com/en/latest/integrations/promptlayer.html\n- https://python.langchain.com/en/latest/modules/models/llms/integrations/promptlayer_openai.html\n- https://python.langchain.com/en/latest/modules/models/chat/integrations/promptlayer_chatopenai.html'}

We can format responses nicely like so:

[16]
https://python.langchain.com/docs/modules/model_io/output_parsers/
[15]
- https://python.langchain.com/en/latest/modules/prompts/output_parsers.html
- https://python.langchain.com/docs/modules/model_io/output_parsers/
- https://python.langchain.com/en/latest/modules/prompts/output_parsers/examples/retry.html