Skip to content

Commit

Permalink
Merge pull request #560 from ryubidragonfire/chyam/06
Browse files Browse the repository at this point in the history
06, added githubmodels-app.py, updated githubmodels-assignment.ipynb
  • Loading branch information
koreyspace committed Sep 3, 2024
2 parents a0e94d6 + 1f75679 commit 9b579dc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .devcontainer/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: <environment-name>
channels:
- defaults
dependencies:
- python=3.12
- openai
- python-dotenv
7 changes: 5 additions & 2 deletions 00-course-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cd generative-ai-for-beginners
Once you have everything checked out, you can get started!

### Installing Miniconda (optional step)

[Miniconda](https://conda.io/en/latest/miniconda.html?WT.mc_id=academic-105485-koreyst) is a lightweight installer for installing [Conda](https://docs.conda.io/en/latest?WT.mc_id=academic-105485-koreyst), Python, as well as a few packages.
Conda itself is a package manager, that makes it easy to setup and switch between different Python [**virtual environments**](https://docs.python.org/3/tutorial/venv.html?WT.mc_id=academic-105485-koreyst) and packages. It also comes in handy for installing packages that are not available via `pip`.

Expand All @@ -48,6 +49,7 @@ With Miniconda installed, you need to clone the [repository](https://github.com/
Next, you need to create a virtual environment. To do this with Conda, go ahead and create a new environment file (_environment.yml_). If you are following along using Codespaces, create this within the `.devcontainer` directory, thus `.devcontainer/environment.yml`.

Go ahead and populate your environment file with the snippet below:

```yml
name: <environment-name>
channels:
Expand All @@ -56,13 +58,14 @@ dependencies:
- python=<python-version>
- openai
- python-dotenv
- azure-ai-inference

```
The environment file specifies the dependencies we need. `<environment-name>` refers to the name you would like to use for your Conda environment, and `<python-version>` is the version of Python you would like to use, for example, `3` is the latest major version of Python.

With that done, you can go ahead and create your Conda environment by running the commands below in your command line/terminal


```bash
conda env create --name ai4beg --file .devcontainer/environment.yml # .devcontainer sub path applies to only Codespace setups
conda activate ai4beg
Expand All @@ -78,7 +81,7 @@ We recommend using the [Visual Studio Code (VS Code)](http://code.visualstudio.c

> **Note**: Once you clone and open the directory in VS Code, it will automatically suggest you install a Python support extension.

> **Note**: If VS Code suggests you re-open the repository in a container, decline this request in order to use the locally installed version of Python.
> **Note**: If VS Code suggests you re-open the repository in a container, decline this request in order to use the locally installed version of Python.

### Using Jupyter in the Browser

Expand Down
36 changes: 36 additions & 0 deletions 06-text-generation-apps/python/githubmodels-app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential

token = os.environ["GITHUB_TOKEN"]
endpoint = "https://models.inference.ai.azure.com"

model_name = "gpt-4o"

client = ChatCompletionsClient(
endpoint=endpoint,
credential=AzureKeyCredential(token),
)

prompt = "Show me 5 recipes for a dish with the following ingredients: chicken, potatoes, and carrots. Per recipe, list all the ingredients used"

response = client.complete(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": prompt,
},
],
model=model_name,
# Optional parameters
temperature=1.,
max_tokens=1000,
top_p=1.
)

print(response.choices[0].message.content)
24 changes: 16 additions & 8 deletions 06-text-generation-apps/python/githubmodels-assignment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,10 @@
"* From the 'Get started' window choose Codespaces\n",
"* Create a new codespace (or use an existing)\n",
"* VS Code will open in your browser with a set of sample notebooks in multiple languages you can try\n",
"* Under ```samples/python/azure_ai_inference``` select ```getting_started.ipynb``` - run through the sample. \n",
"* Run the sample ```./githubmodels-app.py```. \n",
"\n",
"> Note: In codespaces there is no need to set the Github Token variable, skip this step\n",
"\n",
"* Once you have run through all the capabilities and sample code in the notebook lets take the ```basic.py``` and start editing for your own example. Navigate to ```samples/python/azure_ai_inference/``` and enter ```python basic.py``` into the bash terminal\n",
"\n",
"![Codespaces showing running basic.py file and output](../images/codespaces-basicpy.png?WT.mc_id=academic-105485-koreyst)\n",
"\n",
"**Now move to 'Generate Text' section below to continue this assignment**\n",
"\n",
"### 2. VS Code (or any favorite IDE)\n",
Expand Down Expand Up @@ -161,7 +157,7 @@
"## Generate text with ChatCompletions\n",
"\n",
"The way to generate text is to use the `ChatCompletionsClient` class. \n",
"In your code file update the response for the user role by changing the content parameter to below:\n",
"In `samples/python/azure_ai_inference/basic.py`, in the response section of code, update the code the user role by changing the content parameter to below:\n",
"\n",
"```python\n",
"\n",
Expand Down Expand Up @@ -367,9 +363,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'azure'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mazure\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ChatCompletionsClient\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mazure\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodels\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SystemMessage, UserMessage\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mazure\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcredentials\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AzureKeyCredential\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'azure'"
]
}
],
"source": [
"import os\n",
"from azure.ai.inference import ChatCompletionsClient\n",
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pandas==1.5.3
tqdm==4.66.3
python-dotenv==1.0.0
openai>=0.28.0
tiktoken
tiktoken
azure-ai-inference

0 comments on commit 9b579dc

Please sign in to comment.