Skip to content

Commit 997d197

Browse files
author
Chris
committed
cleanup, adding notebook
1 parent c20aee4 commit 997d197

File tree

2 files changed

+961
-35
lines changed

2 files changed

+961
-35
lines changed

6-text-generation-apps/README.md

+44-35
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ At the end of this lesson, you'll be able to:
2020
- Build a text generation app using openai.
2121
- Configure your app to use more or less tokens and also change the temperature, for a varied output.
2222

23-
## References
24-
25-
TODO, openai
26-
27-
## Learning goals
28-
29-
- Understand what a text generation app is.
30-
- Explain what semantic kernel is and how you can use it to build a text generation app.
31-
3223
## What is a text generation app?
3324

3425
Normally when you build an app it some kind of interface like the following:
@@ -76,47 +67,67 @@ Then there are libraries that operate on a higher level like:
7667
- **Semantic Kernel**. Semantic Kernel is a library by Microsoft supporting the languages C#, Python, and Java.
7768

7869
## First app using openai
70+
7971
Let's see how we can build our first app, what libraries we need, how much is required and so on.
72+
8073
### Install openai
74+
8175
There are many libraries out there for interacting with OpenAI or Azure OpenAI. It's possible to use numerous programming languages as well like C#, Python, JavaScript, Java and more. We've chosen to use the `openai` Python library, so we'll use `pip` to install it.
76+
8277
```bash
8378
pip install openai
8479
```
8580

86-
### Create an account
81+
### Create a resource
8782

88-
Go to https://beta.openai.com/ and create an account.
83+
You need to carry out the following steps:
8984

90-
TODO, add instructions for Azure.
85+
- Create an account on Azure <https://azure.microsoft.com/free/>.
86+
- Gain access to Azure Open AI. Go to <https://learn.microsoft.com/en-us/azure/ai-services/openai/overview#how-do-i-get-access-to-azure-openai> and request access.
9187

92-
### Setup your API key
88+
> [!NOTE]
89+
> At the time of writing, you need to apply for access to Azure Open AI.
9390
94-
You can use one of two ways to setup your API key:
91+
- Install Python <https://www.python.org/>
92+
- Have created an Azure OpenAI Service resource. See this guide for how to [create a resource](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal).
9593

96-
- Set the environment variable `OPENAI_API_KEY` to your API key.
9794

98-
```bash
99-
export OPENAI_API_KEY='sk-...'
100-
```
101-
- Set it in code:
95+
### Locate API key and endpoint
10296

103-
```python
104-
import openai
105-
openai.api_key = "sk-..."
106-
```
97+
At this point, you need to tell your `openai` library what API key to use. To find your API key, go to "Keys and Endpoint" section of your Azure Open AI resource and copy the "Key 1" value.
10798

108-
Once you're setup with your API key, you can start using the library to generate text.
99+
![Keys and Endpoint resource blade in Azure Portal](https://learn.microsoft.com/en-us/azure/ai-services/openai/media/quickstarts/endpoint.png)
109100

110-
If you're using Azure Open AI, here's how setup configuration:
101+
Now that you have this information copied, let's instruct the libraries to use it.
111102

112-
```python
113-
import openai
103+
> [!NOTE]
104+
> It's worth separating your API key from your code. You can do so by using environment variables.
105+
> - Set the environment variable `OPENAI_API_KEY` to your API key.
106+
> `export OPENAI_API_KEY='sk-...'`
107+
108+
109+
### Setup configuration Azure
114110

111+
If you're using Azure Open AI, here's how you setup configuration:
112+
113+
```python
115114
openai.api_type = 'azure'
116115
openai.api_key = os.environ["OPENAI_API_KEY"]
116+
openai.api_version = '2023-05-15'
117+
openai.api_base = os.getenv("API_BASE")
117118
```
118119

119-
### Generate text
120+
Above we're setting the following:
121+
122+
- `api_type` to `azure`. This tells the library to use Azure Open AI and not OpenAI.
123+
- `api_key`, this is your API key found in the Azure Portal.
124+
- `api_version`, this is the version of the API you want to use. At the time of writing, the latest version is `2023-05-15`.
125+
- `api_base`, this is the endpoint of the API. You can find it in the Azure Portal next to your API key.
126+
127+
> [!NOTE]
128+
> `os.getenv` is a function that reads environment variables. You can use it to read environment variables like `OPENAI_API_KEY` and `API_BASE`. Set these environment variables in your terminal or by using a library like `dotenv`.
129+
130+
## Generate text
120131

121132
The way to generate text is to use the `Completion` class. Here's an example:
122133

@@ -168,19 +179,17 @@ Now that we learned how to setup and configure openai, it's time to build your f
168179
import openai
169180
170181
openai.api_key = "<replace this value with your open ai key or Azure Open AI key>"
171-
# azure
172182
173-
# enable below if you use Azure Open AI
174-
# openai.api_type = 'azure'
175-
# openai.api_version = '2023-05-15'
176-
# openai.api_base = "<endpoint found in Azure Portal where your API key is>"
177-
183+
openai.api_type = 'azure'
184+
openai.api_version = '2023-05-15'
185+
openai.api_base = "<endpoint found in Azure Portal where your API key is>"
186+
deployment_name = "<deployment name>"
178187
179188
# add your completion code
180189
prompt = "Complete the following: Once upon a time there was a"
181190
182191
# make completion
183-
completion = openai.Completion.create(model="davinci-002", prompt=prompt)
192+
completion = openai.Completion.create(engine= deployment_name, model="davinci-002", prompt=prompt)
184193
185194
# print response
186195
print(completion.choices[0].text)

0 commit comments

Comments
 (0)