You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/1_call_azure_openai_chat/README.md
+62-2Lines changed: 62 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,63 @@
1
1
# Call Azure OpenAI Service API from Python
2
2
3
-
This article explains how to call Azure OpenAI Service API from Python.
3
+
This app demonstrates how to call the Azure OpenAI Service API from Python.
4
4
5
5
## Prerequisites
6
6
7
7
- Python 3.10 or later
8
8
- Azure OpenAI Service
9
9
10
+
## Overview
11
+
12
+
To call Azure OpenAI Service API, you can send HTTP requests directly to the API endpoint or use the [OpenAI Python API library](https://pypi.org/project/openai/).
13
+
14
+
**Send HTTP requests directly to the API endpoint**
To call the Azure OpenAI Service API, run the following command.
85
+
86
+
Detailed information is described in the [Quickstart: Get started using GPT-35-Turbo and GPT-4 with Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python).
-[Quickstart: Get started using GPT-35-Turbo and GPT-4 with Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python)
-[venv — Creation of virtual environments](https://docs.python.org/3/library/venv.html#creating-virtual-environments)
176
+
- Azure OpenAI Basics
177
+
-[Azure OpenAI Service documentation](https://learn.microsoft.com/azure/ai-services/openai/)
178
+
-[Quickstart: Get started generating text using Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python)
Copy file name to clipboardExpand all lines: apps/2_streamlit_chat/README.md
+76-4Lines changed: 76 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,82 @@ This app demonstrates how to create a chat application using Azure OpenAI Servic
7
7
- Python 3.10 or later
8
8
- Azure OpenAI Service
9
9
10
+
## Overview
11
+
12
+
In this app, you learn how to create an app with [Streamlit](https://streamlit.io/).
13
+
14
+
Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science projects. It allows you to build interactive applications with minimal code (only in Python!!), focusing on rapid prototyping and simplicity.
15
+
16
+
To get started, it is recommended to go through the [Streamlit documentation](https://docs.streamlit.io/get-started/installation/command-line) to create your first hello world app.
17
+
18
+
```shell
19
+
# Go to some directory
20
+
cd tmp
21
+
22
+
# Create a virtual environment
23
+
python -m venv .venv
24
+
25
+
# Activate the virtual environment
26
+
source .venv/bin/activate
27
+
28
+
# Install Streamlit
29
+
pip install streamlit
30
+
31
+
# Run the hello world python script from local file, described below
32
+
streamlit run ./hello.py
33
+
```
34
+
35
+
**Create a "Hello World" app and run it**
36
+
37
+
```python
38
+
import streamlit as st
39
+
40
+
st.write("Hello world")
41
+
```
42
+
43
+
To go further, you can refer to the [API reference](https://docs.streamlit.io/develop/api-reference) to understand the different components and functionalities available in Streamlit. You can also refer to the [Streamlit Cheat Sheet](https://docs.streamlit.io/develop/quick-reference/cheat-sheet). There are many examples and tutorials available on the Streamlit website.
44
+
45
+
**Create a chat application**
46
+
47
+
For implementing the chat application, you can refer to the [Streamlit Chat](https://streamlit.io/generative-ai) example below.
48
+
This app uses the OpenAI API to create a chatbot that can chat with users. The chatbot uses the GPT-3.5-turbo model to generate responses.
49
+
50
+
```python
51
+
import streamlit as st
52
+
from openai import OpenAI
53
+
54
+
with st.sidebar:
55
+
openai_api_key = st.text_input("OpenAI API Key", key="chatbot_api_key", type="password")
56
+
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
57
+
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/Chatbot.py)"
58
+
"[](https://codespaces.new/streamlit/llm-examples?quickstart=1)"
59
+
60
+
st.title("💬 Chatbot")
61
+
62
+
if"messages"notin st.session_state:
63
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
### **_Exercise: Convert OpenAI API to Azure OpenAI Service_**
83
+
84
+
In this exercise, please convert the OpenAI API to the Azure OpenAI Service API with the knowledge you have gained from the previous chapter [1_call_azure_openai_chat](../1_call_azure_openai_chat/README.md).
85
+
10
86
## Usage
11
87
12
88
1. Get Azure OpenAI Service API key
@@ -41,7 +117,3 @@ To start a conversation, fill in the required fields in the sidebar and you will
41
117
## Note
42
118
43
119
This app uses `st.session_state.messages` to store messages for chat. This is a mechanism to store messages per session on the process side of the application. Messages will disappear when the session ends.
Copy file name to clipboardExpand all lines: apps/6_call_azure_ai_search/README.md
+52-8Lines changed: 52 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,54 @@ This application explains how to call Azure AI Search from Python.
8
8
- Azure AI Search
9
9
- Azure OpenAI Service
10
10
11
+
## Overview
12
+
13
+
Azure AI Search (formerly known as Azure Cognitive Search) is a fully managed cloud search service that provides information retrieval over user-owned content.
14
+
Data plane REST APIs are used for indexing and query workflows, and are documented in this section.
15
+
[Azure AI Search REST API reference](https://learn.microsoft.com/rest/api/searchservice/?view=rest-searchservice-2023-11-01) provides detailed information about the APIs.
16
+
17
+
REST API specs in OpenAPI format are available in the [Azure/azure-rest-api-specs](https://github.com/Azure/azure-rest-api-specs/tree/main/specification/search/data-plane/Azure.Search) repository.
18
+
19
+
[Samples for Azure Cognitive Search client library for Python](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/search/azure-search-documents/samples) are available in the Azure SDK for Python repository.
20
+
21
+
[Azure AI Search client library for Python - version 11.5.1](https://learn.microsoft.com/en-us/python/api/overview/azure/search-documents-readme?view=azure-python) provides primitive APIs for working with Azure AI Search. It is flexible and allows you to work with the service at a lower level.
22
+
23
+
**Introducing LangChain**
24
+
25
+
[LangChain](https://github.com/langchain-ai/langchain) is a framework for developing applications powered by large language models (LLMs).
26
+
It provides a set of tools and libraries to help you build, train, and deploy LLMs in production.
On the other hand, for example, the OpenAI Python SDK provides a direct interface to OpenAI's API, enabling developers to integrate OpenAI's powerful language models into their applications
31
+
32
+
The relationship between LangChain and the OpenAI Python SDK is complementary. LangChain leverages the OpenAI Python SDK to access and utilize OpenAI's models, providing a higher-level abstraction that simplifies the integration of these models into more complex workflows and applications.
33
+
34
+
**Use LangChain to access Azure AI Search easily**
35
+
36
+
[Azure AI Search](https://python.langchain.com/v0.2/docs/integrations/vectorstores/azuresearch/) interface in LangChain provides a simple and easy way to access Azure AI Search from Python.
37
+
38
+
**Use RecursiveCharacterTextSplitter to recursively split text by characters**
39
+
40
+
It is necessary to split text by characters when you need to put text into a search index.
41
+
Implementing text splitting by characters is a common task in natural language processing (NLP) and information retrieval (IR) applications but it is tedious and error-prone.
42
+
So we introduce `RecursiveCharacterTextSplitter` which provides a simple and easy way to recursively split text by characters. Details are available in the following link.
43
+
44
+
-[How to recursively split text by characters](https://python.langchain.com/v0.2/docs/how_to/recursive_text_splitter/)
45
+
11
46
## Usage
12
47
13
48
1. Get the API key for Azure AI Search
14
49
1. Copy [.env.template](../../.env.template) to `.env` in the same directory
15
50
1. Set credentials in `.env`
16
51
1. Run scripts in the [apps/6_call_azure_ai_search](./) directory
17
52
53
+
> [!CAUTION]
54
+
> `AZURE_AI_SEARCH_INDEX_NAME` in `.env` should be unique and should not be changed once set.
55
+
> If you change the index name, you will need to recreate the index and re-upload the documents.
56
+
57
+
Set up the environment and install dependencies:
58
+
18
59
```shell
19
60
# Create a virtual environment
20
61
$ python -m venv .venv
@@ -24,20 +65,23 @@ $ source .venv/bin/activate
24
65
25
66
# Install dependencies
26
67
$ pip install -r requirements.txt
68
+
```
69
+
70
+
Create an index in Azure AI Search and upload documents:
71
+
72
+
> [!CAUTION]
73
+
> This script should be run only once to avoid creating duplicate indexes.
27
74
28
-
# Create an index in Azure AI Search and upload documents
29
-
# Note: This script should be run only once to avoid creating duplicate indexes
Copy file name to clipboardExpand all lines: apps/7_streamlit_chat_rag/README.md
+40-1Lines changed: 40 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,41 @@
1
1
# Add RAG feature to Streamlit chat app
2
2
3
-
This app demonstrates how to add a feature to save chat history using Azure Cosmos DB to an Azure OpenAI Chat app using Streamlit.
3
+
This app demonstrates how to add the Retrieval-Augmented Generation (RAG) feature to a Streamlit chat app.
4
4
5
5
## Prerequisites
6
6
7
7
- Python 3.10 or later
8
8
- Azure OpenAI Service
9
9
- Azure AI Search
10
10
11
+
## Overview
12
+
13
+
**What is RAG?**
14
+
15
+
Retrieval-Augmented Generation (RAG) is a model that combines the strengths of retrieval and generation models.
16
+
It uses a retriever to find relevant passages from a large corpus and then uses a generator to generate a response based on the retrieved passages.
17
+
In this way, RAG can generate more accurate and informative responses than traditional generation models.
18
+
This chapter provides an practical example of how to use RAG in a Streamlit chat app with the knowledge that you've learned in the following previous chapters.
19
+
20
+
- LLM: Azure OpenAI Service @ [1_call_azure_openai_chat](../1_call_azure_openai_chat/README.md)
Function calling is a technical feature that allows you to connect LLM models to external tools and systems. This is useful for many things such as empowering AI assistants with capabilities, or building deep integrations between your applications and the models.
31
+
32
+
For example, if you want to implement chat app with RAG feature, you can use the function calling feature to connect the LLM model to external knowledge bases or search engines. This allows the model to retrieve relevant information from the knowledge base or search engine and generate a response based on that information.
33
+
34
+
**Introduce `AgentExecutor` to introduce function calling**
35
+
36
+
Implementing Function Calling from scratch can be complex and time-consuming. To make it easier, LangChain provides a feature called `AgentExecutor`.
37
+
AgentExecutor is a feature that allows you to connect LLM models to external tools and systems. This feature enables you to build deep integrations between your applications and the models, and empower AI assistants with capabilities.
38
+
11
39
## Usage
12
40
13
41
1. Get Azure OpenAI Service API key
@@ -37,3 +65,14 @@ Access `http://localhost:8501` and set the required fields in the sidebar to sta
37
65
When you send a question about Contoso Corporation, the chatbot will respond with an answer from Azure AI Search.
0 commit comments