Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
koreyspace authored Feb 16, 2024
2 parents dac51a5 + 118b4ca commit 74a0fea
Show file tree
Hide file tree
Showing 23 changed files with 481 additions and 87 deletions.
1 change: 1 addition & 0 deletions .env.copy
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ AZURE_OPENAI_KEY='<add your key here>'
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT='<add your deployment name here>'

# OpenAI Configuration

OPENAI_API_KEY='<add your OpenAI key here>'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ We have instrumented this repository with a _dev container_ that comes with a Py

## 1. Create `.env` file

The default notebook is set up for use with an [Azure OpenAI service resource](https://learn.microsoft.com/azure/ai-services/openai?WT.mc_id=academic-105485-koreyst). To configure this, we need to setup local environment variables for Azure as follows:
The default notebook (identified by the 'aoai-' suffix) is set up for use with an [Azure OpenAI service resource](https://learn.microsoft.com/azure/ai-services/openai?WT.mc_id=academic-105485-koreyst). However, you have the option to run your assignments by using non-Azure openAI endpoints (choose the 'oai-' prefixed notebooks in this case).

To configure this, we need to setup local environment variables for Azure as follows:

1. Look in the root folder for a `.env.copy` file. It should contain a list of name-value pairs like this:

Expand All @@ -23,6 +25,12 @@ The default notebook is set up for use with an [Azure OpenAI service resource](h

3. (Option) If you use GitHub Codespaces, you have the option to save environment variables as _Codespaces secrets_ associated with this repository. In that case, you won't need to setup a local .env file. **However, note that this option works only if you use GitHub Codespaces.** You will still need to setup the .env file if you use Docker Desktop instead.
The above steps should be executed also if you are using the non-Azure OpenAI endpoints. In that case, you will need to populate the .env file with the appropriate values for the OpenAI service.
```bash
OPENAI_API_KEY='<add your OpenAI key here>'
```
## 2. Populate `.env` file
Expand All @@ -32,6 +40,7 @@ Let's take a quick look at the variable names to understand what they represent:
|:---|:---|
|AZURE_OPENAI_ENDPOINT| This is the deployed endpoint for an Azure OpenAI resource|
|AZURE_OPENAI_KEY | This is the authorization key for using that service |
|OPENAI_API_KEY | This is the authorization key for using the service for non-Azure OpenAI endpoints |
|AZURE_OPENAI_DEPLOYMENT| This is the _text generation_ model deployment endpoint |
|AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT | This is the _text embeddings_ model deployment endpoint |
| | |
Expand Down Expand Up @@ -69,4 +78,7 @@ AZURE_OPENAI_DEPLOYMENT='gpt-35-turbo'
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT='text-embedding-ada-002'
```

**Don't forget to save the .env file when done**. You can now exit the file and return to the instructions for running the notebook.
**Don't forget to save the .env file when done**. You can now exit the file and return to the instructions for running the notebook.
### 2.3 Use OpenAI Public API
Your OpenAI API key can be found in your [OpenAI account](https://platform.openai.com/api-keys?WT.mc_id=academic-105485-koreyst). If you don't have one, you can sign up for an account and create an API key. Once you have the key, you can use it to populate the `OPENAI_API_KEY` variable in the `.env` file.
22 changes: 11 additions & 11 deletions 07-building-chat-applications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ When building a chat application, a great first step is to assess what is alread
- **Easier maintenance**: Updates and improvements are easier to manage as most APIs and SDKs simply require an update to a library when a newer version is released.
- **Access to cutting edge technology**: Leveraging models that have been fined tuned and trained on extensive datasets provides your application with natural language capabilities.

Accessing functionality of an SDK or API typically involves obtaining permission to use the provided services, which is often through the use of a unique key or authentication token. We'll use the OpenAI Python Library to explore what this looks like. You can also try it out on your own in the following [notebook for OpenAI](./notebook-openai.ipynb?WT.mc_id=academic-105485-koreyst) or [notebook for Azure OpenAI Services](./notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreys) for this lesson.
Accessing functionality of an SDK or API typically involves obtaining permission to use the provided services, which is often through the use of a unique key or authentication token. We'll use the OpenAI Python Library to explore what this looks like. You can also try it out on your own in the following [notebook for OpenAI](./python/oai-assignment.ipynb?WT.mc_id=academic-105485-koreyst) or [notebook for Azure OpenAI Services](./python/aoai-assignment.ipynb?WT.mc_id=academic-105485-koreys) for this lesson.

```python
import os
import openai
from openai import OpenAI

openai.api_key = os.getenv("OPENAI_API_KEY")
API_KEY = os.getenv("OPENAI_API_KEY","")

chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}])
```

The above example uses the GPT-3.5 Turbo model to complete the prompt, but notice that the API key is set prior to doing so. You'd receive the following error if you didn't set the key.
client = OpenAI(
api_key=API_KEY
)

```output
AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.
chat_completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}])
```

The above example uses the GPT-3.5 Turbo model to complete the prompt, but notice that the API key is set prior to doing so. You'd receive an error if you didn't set the key.

## User Experience (UX)

General UX principles apply to chat applications, but here are some additional considerations that become particularly important due to the machine learning components involved.
Expand Down Expand Up @@ -128,7 +128,7 @@ Fine-tuning is often considered when a pre-trained model falls short in a specia

For instance, medical queries are complex and require a lot of context. When a medical professional diagnoses a patient it's based on a variety of factors such as lifestyle or pre-existing conditions, and may even rely on recent medical journals to validate their diagnosis. In such nuanced scenarios, a general-purpose AI chat application cannot be a reliable source.

### Scenario: a medical application**
### Scenario: a medical application

Consider a chat application designed to assist medical practitioners by providing quick references to treatment guidelines, drug interactions, or recent research findings.

Expand Down Expand Up @@ -175,7 +175,7 @@ Microsoft's approach to Responsible AI has identified six principles that should

## Assignment

See [assignment](./notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreyst) it will take you through a series of exercises from running your first chat prompts, to classifying and summarizing text and more.
See [assignment](./python?WT.mc_id=academic-105485-koreyst) it will take you through a series of exercises from running your first chat prompts, to classifying and summarizing text and more. Notice that the assignments are available in different programming languages!

## Great Work! Continue the Journey

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.13"
},
"orig_nbformat": 4
},
Expand Down
65 changes: 65 additions & 0 deletions 07-building-chat-applications/python/oai-assigment-simple.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from openai import OpenAI\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"\n",
"API_KEY = os.getenv(\"OPENAI_API_KEY\",\"\").strip()\n",
"assert API_KEY, \"ERROR: OpenAI Key is missing\"\n",
"client = OpenAI(\n",
" api_key=API_KEY\n",
" )\n",
"\n",
"model = \"gpt-3.5-turbo\" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create your first prompt\n",
"text_prompt = \" My foot hurts, what can be wrong?\"\n",
"\n",
"response = client.chat.completions.create(\n",
" model=model,\n",
" messages = [\n",
" {\"role\":\"system\", \"content\":\"I'm a doctor, specialist on surgery\"},\n",
" {\"role\":\"user\",\"content\":text_prompt},])\n",
"\n",
"response.choices[0].message.content"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.10.13"
},
"microsoft": {
"host": {
Expand Down
14 changes: 9 additions & 5 deletions 07-building-chat-applications/translations/cn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@
- **更容易维护**:更新和改进更易于管理,因为大多数 API 和 SDK 在发布新版本时只需要更新库。
- **获得尖端技术**:利用经过微调和在广泛数据集上训练的模型为您的应用程序提供自然语言功能。

访问 SDK 或 API 的功能通常涉及获取使用所提供服务的许可,这通常是通过使用唯一 kwy 或身份验证 token 来实现的。 我们将使用 OpenAI Python library 来探索它是什么样子。 您也可以在本章的[notebook](../../notebook-openai.ipynb?WT.mc_id=academic-105485-koreyst) 中自行尝试。
访问 SDK 或 API 的功能通常涉及获取使用所提供服务的许可,这通常是通过使用唯一 kwy 或身份验证 token 来实现的。 我们将使用 OpenAI Python library 来探索它是什么样子。 您也可以在本章的[notebook](../../python/oai-assignment.ipynb?WT.mc_id=academic-105485-koreyst) 中自行尝试。

```python
import os
import openai
from openai import OpenAI

openai.api_key = os.getenv("OPENAI_API_KEY")
API_KEY = os.getenv("OPENAI_API_KEY","")

chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}])
client = OpenAI(
api_key=API_KEY
)

chat_completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}])
```

上面的示例使用 GPT-3.5 Turbo 模型来完成提示,但请注意,API 密钥是在执行此操作之前设置的。 如果未设置 key,您将收到以下错误。
Expand Down Expand Up @@ -178,7 +182,7 @@ Microsoft 的 Responsible AI 方法确定了指导 AI 开发和使用的六项

## 作业

请查看[作业](../../notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreyst),它将引导您完成一系列练习,从运行第一个聊天提示到对文本进行分类和总结等等。
请查看[作业](../../python?WT.mc_id=academic-105485-koreyst),它将引导您完成一系列练习,从运行第一个聊天提示到对文本进行分类和总结等等。

## Great Work! Continue the Journey

Expand Down
14 changes: 9 additions & 5 deletions 07-building-chat-applications/translations/ja-jp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@
- **メンテナンスの容易さ**: ほとんどの API と SDK で、新バージョンがリリースされた際、ライブラリを更新するだけで済むため、アップデートや改善が容易になります。
- **最先端技術へのアクセス**: 大規模なデータセットでファイン・チューニングやトレーニングされたモデルを活用すると、アプリケーションに自然言語処理の能力を追加できます。

SDK や API の機能を利用するためには、通常、提供されるサービスの使用許可を取得する必要があります。これは、一意のキーや認証トークンを使って行われます。OpenAI Python ライブラリを使って、これがどのように行われるのか確認してみましょう。また、このレッスン用の [OpenAI のノートブック](../../notebook-openai.ipynb?WT.mc_id=academic-105485-yoterada)[Azure OpenAI Services のノートブック](../../notebook-azure-openai.ipynb?WT.mc_id=academic-105485-yoterada)を使って、自分で試せます。
SDK や API の機能を利用するためには、通常、提供されるサービスの使用許可を取得する必要があります。これは、一意のキーや認証トークンを使って行われます。OpenAI Python ライブラリを使って、これがどのように行われるのか確認してみましょう。また、このレッスン用の [OpenAI のノートブック](../../python/oai-assignment.ipynb?WT.mc_id=academic-105485-yoterada)[Azure OpenAI Services のノートブック](../../python/aoai-assignment.ipynb?WT.mc_id=academic-105485-yoterada)を使って、自分で試せます。

```python
import os
import openai
from openai import OpenAI

openai.api_key = os.getenv("OPENAI_API_KEY")
API_KEY = os.getenv("OPENAI_API_KEY","")

chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "生成 AI のチャット・アプリケーションに関するトレーニング・タイトルを2つ提案してください。"}])
client = OpenAI(
api_key=API_KEY
)

chat_completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "生成 AI のチャット・アプリケーションに関するトレーニング・タイトルを2つ提案してください。"}])
```

上記の例では、GPT-3.5 Turbo モデルを使用してプロンプトを作成していますが、その前に API キーを設定している点にご注意ください。キーを設定しなかった場合は、下記のエラーが表示されます。
Expand Down Expand Up @@ -209,7 +213,7 @@ Microsoft の責任ある AI への取り組みでは、AI の開発と利用を

## 課題

[課題](../../notebook-azure-openai.ipynb?WT.mc_id=academic-105485-yoterada)をご覧ください。最初のチャット・プロンプトの実行から、テキストの分類や要約など、一連の演習を行えます。
[課題](../../python?WT.mc_id=academic-105485-yoterada)をご覧ください。最初のチャット・プロンプトの実行から、テキストの分類や要約など、一連の演習を行えます。

## お疲れ様でした! 学習を続ける

Expand Down
14 changes: 9 additions & 5 deletions 07-building-chat-applications/translations/pt-br/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ Ao criar uma aplicação de chat, um ótimo primeiro passo é avaliar o que já

- **Acesso a tecnologia de ponta**: Alavancar modelos que foram ajustados e treinados em conjuntos de dados extensivos proporciona à sua aplicação capacidades de linguagem natural.

Acesso à funcionalidade de um SDK ou API geralmente envolve a obtenção de permissão para usar os serviços fornecidos, frequentemente por meio do uso de uma chave única ou token de autenticação. Utilizaremos a Biblioteca Python da OpenAI para explorar como isso é feito. Você também pode experimentar por conta própria no seguinte [notebook para OpenAI](../../notebook-openai.ipynb?WT.mc_id=academic-105485-koreyst) ou [Notebook para Serviços Azure OpenAI](../../notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreys) para esta lição.
Acesso à funcionalidade de um SDK ou API geralmente envolve a obtenção de permissão para usar os serviços fornecidos, frequentemente por meio do uso de uma chave única ou token de autenticação. Utilizaremos a Biblioteca Python da OpenAI para explorar como isso é feito. Você também pode experimentar por conta própria no seguinte [notebook para OpenAI](../../python/oai-assigment-simple.ipynb?WT.mc_id=academic-105485-koreyst) ou [Notebook para Serviços Azure OpenAI](../../python/aoai-assignment.ipynb?WT.mc_id=academic-105485-koreys) para esta lição.

```python
import os
import openai
from openai import OpenAI

openai.api_key = os.getenv("OPENAI_API_KEY")
API_KEY = os.getenv("OPENAI_API_KEY","")

chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}])
client = OpenAI(
api_key=API_KEY
)

chat_completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Suggest two titles for an instructional lesson on chat applications for generative AI."}])
```

No exemplo acima é usado o modelo GPT-3.5 Turbo para completar o prompt. Mas observe que a chave da API é definida antes de fazê-lo. Você receberia o seguinte erro se não definisse a chave.
Expand Down Expand Up @@ -183,7 +187,7 @@ A abordagem da Microsoft para a IA Responsável identificou seis princípios que

## Tarefa

Veja a [tarefa](../../notebook-azure-openai.ipynb?WT.mc_id=academic-105485-koreyst) que o levará por uma série de exercícios, desde a execução de seus primeiros prompts de chat até a classificação e resumo de texto e muito mais.
Veja a [tarefa](../../python?WT.mc_id=academic-105485-koreyst) que o levará por uma série de exercícios, desde a execução de seus primeiros prompts de chat até a classificação e resumo de texto e muito mais.

## Excelente Trabalho! Continue no Aprendizado!

Expand Down
2 changes: 1 addition & 1 deletion 08-building-search-applications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ az cognitiveservices account deployment create \

## Solution

Open the [solution notebook](./solution.ipynb?WT.mc_id=academic-105485-koreyst) in GitHub Codespaces and follow the instructions in the Jupyter Notebook.
Open the [solution notebook](./python/aoai-solution.ipynb?WT.mc_id=academic-105485-koreyst) in GitHub Codespaces and follow the instructions in the Jupyter Notebook.

When you run the notebook, you'll be prompted to enter a query. The input box will look like this:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install openai dotenv"
"%pip install openai python-dotenv"
]
},
{
Expand Down Expand Up @@ -35,7 +35,7 @@
"outputs": [],
"source": [
"# Dependencies for embeddings_utils\n",
"!pip install matplotlib plotly scikit-learn pandas"
"%pip install matplotlib plotly scikit-learn pandas"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"model = os.environ['AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT']\n",
"\n",
"SIMILARITIES_RESULTS_THRESHOLD = 0.75\n",
"DATASET_NAME = \"embedding_index_3m.json\""
"DATASET_NAME = \"../embedding_index_3m.json\""
]
},
{
Expand Down
Loading

0 comments on commit 74a0fea

Please sign in to comment.