Skip to content

Commit

Permalink
Adding support to non-Azure OpenAI endpoints, applying new folder str…
Browse files Browse the repository at this point in the history
…ucture
  • Loading branch information
carlotta94c committed Feb 12, 2024
1 parent c586374 commit 11239ce
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .env.copy
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Azure OpenAI configuration
AZURE_OPENAI_ENDPOINT='<add your endpoint here>'
AZURE_OPENAI_DEPLOYMENT='<add your deployment name here>'
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>'
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

0 comments on commit 11239ce

Please sign in to comment.