Skip to content

Commit b0404e1

Browse files
committed
update docs
1 parent cda8803 commit b0404e1

File tree

5 files changed

+233
-15
lines changed

5 files changed

+233
-15
lines changed

apps/1_call_azure_openai_chat/README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,63 @@
11
# Call Azure OpenAI Service API from Python
22

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.
44

55
## Prerequisites
66

77
- Python 3.10 or later
88
- Azure OpenAI Service
99

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**
15+
16+
```shell
17+
YOUR_AOAI_NAME="your-aoai-name"
18+
YOUR_DEPLOYMENT_ID="your-deployment-id"
19+
YOUR_API_KEY="your-api-key"
20+
21+
curl -X 'POST' \
22+
"https://$YOUR_AOAI_NAME.openai.azure.com/openai/deployments/$YOUR_DEPLOYMENT_ID/chat/completions?api-version=2023-12-01-preview" \
23+
-H "api-key: $YOUR_API_KEY" \
24+
-H 'Content-Type: application/json' \
25+
-d '{
26+
"messages": [
27+
{"role": "user", "content": "What is the weather like in Boston and New York?"}
28+
]
29+
}'
30+
```
31+
32+
**Use OpenAI Python API library**
33+
34+
```python
35+
# Import modules
36+
from os import getenv
37+
from openai import AzureOpenAI
38+
39+
# Initialize AzureOpenAI client
40+
client = AzureOpenAI(
41+
api_key=getenv("AZURE_OPENAI_API_KEY"),
42+
api_version=getenv("AZURE_OPENAI_API_VERSION"),
43+
azure_endpoint=getenv("AZURE_OPENAI_ENDPOINT"),
44+
)
45+
46+
# Call completion API and get a response to user input
47+
response = client.chat.completions.create(
48+
model=getenv("AZURE_OPENAI_GPT_MODEL"),
49+
messages=[
50+
{"role": "system", "content": "You are a helpful assistant."},
51+
{"role": "user", "content": "Hello"},
52+
],
53+
)
54+
```
55+
56+
For more information, see the following references.
57+
58+
- API Reference: [Azure OpenAI Service REST API reference](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference)
59+
- OpenAPI Spec: [Cognitive Services AzureOpenAI SDKs](https://github.com/Azure/azure-rest-api-specs/tree/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference).
60+
1061
## Usage
1162

1263
1. Get Azure OpenAI Service API key
@@ -30,6 +81,10 @@ $ python apps/1_call_azure_openai/main.py
3081

3182
### Example
3283

84+
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).
87+
3388
```shell
3489
$ python apps/1_call_azure_openai_chat/main.py
3590
{
@@ -115,4 +170,9 @@ $ python apps/1_call_azure_openai_chat/main.py
115170

116171
## References
117172

118-
- [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)
173+
- Python basics
174+
- [Python Cheatsheet > Basics](https://www.pythoncheatsheet.org/cheatsheet/basics)
175+
- [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)

apps/2_streamlit_chat/README.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,82 @@ This app demonstrates how to create a chat application using Azure OpenAI Servic
77
- Python 3.10 or later
88
- Azure OpenAI Service
99

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+
"[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/streamlit/llm-examples?quickstart=1)"
59+
60+
st.title("💬 Chatbot")
61+
62+
if "messages" not in st.session_state:
63+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
64+
65+
for msg in st.session_state.messages:
66+
st.chat_message(msg["role"]).write(msg["content"])
67+
68+
if prompt := st.chat_input():
69+
if not openai_api_key:
70+
st.info("Please add your OpenAI API key to continue.")
71+
st.stop()
72+
73+
client = OpenAI(api_key=openai_api_key)
74+
st.session_state.messages.append({"role": "user", "content": prompt})
75+
st.chat_message("user").write(prompt)
76+
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=st.session_state.messages)
77+
msg = response.choices[0].message.content
78+
st.session_state.messages.append({"role": "assistant", "content": msg})
79+
st.chat_message("assistant").write(msg)
80+
```
81+
82+
### **_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+
1086
## Usage
1187

1288
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
41117
## Note
42118

43119
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.
44-
45-
## References
46-
47-
- [Your LLM code playground](https://streamlit.io/generative-ai)

apps/2_streamlit_chat/hello.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import streamlit as st
2+
3+
st.write("Hello world")

apps/6_call_azure_ai_search/README.md

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,54 @@ This application explains how to call Azure AI Search from Python.
88
- Azure AI Search
99
- Azure OpenAI Service
1010

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.
27+
28+
![LangChain Framework](https://raw.githubusercontent.com/langchain-ai/langchain/master/docs/static/svg/langchain_stack_062024.svg)
29+
30+
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+
1146
## Usage
1247

1348
1. Get the API key for Azure AI Search
1449
1. Copy [.env.template](../../.env.template) to `.env` in the same directory
1550
1. Set credentials in `.env`
1651
1. Run scripts in the [apps/6_call_azure_ai_search](./) directory
1752

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+
1859
```shell
1960
# Create a virtual environment
2061
$ python -m venv .venv
@@ -24,20 +65,23 @@ $ source .venv/bin/activate
2465

2566
# Install dependencies
2667
$ 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.
2774
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
75+
```shell
3076
$ python apps/6_call_azure_ai_search/1_create_index.py
77+
```
3178

32-
# Search documents in Azure AI Search
79+
Search documents in Azure AI Search:
80+
81+
```shell
3382
$ python apps/6_call_azure_ai_search/2_search_index.py
3483

3584
> All meetings must include a 5-minute meditation session.
3685
> All meetings must begin with a joke.
3786
> All meetings must have a theme, such as pirate or superhero.
3887
```
39-
40-
## References
41-
42-
- [How to recursively split text by characters](https://python.langchain.com/v0.2/docs/how_to/recursive_text_splitter/)
43-
- [LangChain > Azure AI Search](https://python.langchain.com/v0.2/docs/integrations/vectorstores/azuresearch/)

apps/7_streamlit_chat_rag/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
# Add RAG feature to Streamlit chat app
22

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.
44

55
## Prerequisites
66

77
- Python 3.10 or later
88
- Azure OpenAI Service
99
- Azure AI Search
1010

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)
21+
- Chat app: Streamlit @ [2_streamlit_chat](../2_streamlit_chat/README.md)
22+
- Search: Azure AI Search @ [6_call_azure_ai_search](../6_call_azure_ai_search/README.md)
23+
24+
This app just combines the above components to create a chat app with RAG feature.
25+
26+
**Introducing Function calling**
27+
28+
![Function Calling](https://cdn.openai.com/API/docs/images/function-calling-diagram.png)
29+
30+
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+
1139
## Usage
1240

1341
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
3765
When you send a question about Contoso Corporation, the chatbot will respond with an answer from Azure AI Search.
3866

3967
![RAG Chat](../../docs/images/7_streamlit_chat_rag.main.png)
68+
69+
To see how the RAG feature works, watch the video below.
70+
71+
[![Streamlit Chat App with RAG Feature](https://img.youtube.com/vi/ummiu-rzYvs/0.jpg)](https://youtu.be/ummiu-rzYvs)
72+
73+
### How to customize
74+
75+
You can customize the chat app by modifying the following codes:
76+
77+
- [main.py](./main.py): `CUSTOM_SYSTEM_PROMPT` variable which defines the system prompt
78+
- [tools/fetch_contents.py](./tools/fetch_contents.py): `fetch_contents` function comments which is passed to the LLM model

0 commit comments

Comments
 (0)