Skip to content

Instantly share code, notes, and snippets.

@codingoutloud
Last active March 28, 2024 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingoutloud/4a95803080b2d7bf6ffbd461434564d4 to your computer and use it in GitHub Desktop.
Save codingoutloud/4a95803080b2d7bf6ffbd461434564d4 to your computer and use it in GitHub Desktop.
Mini-workshop temporarily useful files
import os
from openai import AzureOpenAI
from datetime import datetime
# TODO: CONFIGURE ENVIRONMENT for PYTHON:
# 1. create empty folder, cd into it
# 2. create and activate isolated python environment:
# python3 -m venv .
# source bin/activate
# 3. install needed Azure OpenAI packages:
# pip3 install openai
# 4. copy this file into that folder - hello-ai.py
# 5. run the app:
# python3 hello-ai.py
# 6. then diagnose and solve the problem of the AI is not responding as instructed
# see below for the challenge details (search for string "CHALLENGE")
# DO NOT DO THIS IN REAL CODE!
# this is a certified TERRIBLE IDEA (tm) - but makes mini-workshop possible
# key will be rolled (invalidated) right after this mini-workshop
deployment = "tdih-gpt-35-turbo-instruct"
client = AzureOpenAI(
api_key="8bfe9d6249454b11850be97393c415e1",
api_version="2024-02-01",
azure_endpoint = "https://tdih.openai.azure.com/"
)
# output today's date just for fun
print(f"----------------- DEBUG INFO -----------------")
today = datetime.now().strftime("%B %d")
print(f"Today is {today}");
print(f"----------------------------------------------");
# TODO: CHALLENGE 1: does the AI respond accurately to this prompt? How to fix?
prompt = "Tell me an interesting fact from world about an event " \
"that took place on today's date. " \
"Be sure to mention the date in history for context. "
max_tokens = 250 # PLEASE DON'T MAKE LARGER THAN 250 (but see what happens at 25)
print(f"PROMPT: \n\n{prompt}")
response = client.completions.create(model=deployment, prompt=prompt, max_tokens=max_tokens)
print(f"\nRESPONSE: {response.choices[0].text}")
print(f"----------------- DEBUG INFO -----------------")
print(f"Tokens used: {response.usage.completion_tokens}/{max_tokens}")
print(f"----------------------------------------------");
using System;
// TODO: CONFIGURE ENVIRONMENT:
// 1. create empty folder
// 2. generate a console app:
// dotnet new console
// 3. install needed Azure OpenAI packages:
// dotnet add package Azure.AI.OpenAI --prerelease
// 4. clobber the contents of Program.cs with the code in this file
// yes, just copy and paste this code into Program.cs or overwrite it
// 5. run the app:
// dotnet run
// 6. then diagnose and solve the problem of the AI is not responding as instructed
// see below for the challenge details (search for string "CHALLENGE")
using Azure;
using Azure.AI.OpenAI;
/* DO NOT DO THIS IN REAL CODE! */
/* this is a certified TERRIBLE IDEA (tm) - but makes mini-workshop possible */
/* key will be rolled (invalidated) right after this mini-workshop */
string? key = "8bfe9d6249454b11850be97393c415e1";
string? endpoint = "https://tdih.openai.azure.com/";
string? deployment = "tdih-gpt-35-turbo-instruct";
// output today's date just for fun
Console.WriteLine($"\n----------------- DEBUG INFO -----------------");
var today = DateTime.Now.ToString("MMMM dd");
Console.WriteLine($"Today is {today}");
Console.WriteLine("----------------------------------------------");
var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
// TODO: CHALLENGE 1: does the AI respond accurately to this prompt? How to fix?
var prompt = $"Tell me an interesting fact from world about an event " +
$"that took place on today's date. " +
$"Be sure to mention the date in history for context. ";
CompletionsOptions completionsOptions = new()
{
DeploymentName = deployment,
Prompts = { prompt },
MaxTokens = 250, // PLEASE DON'T MAKE LARGER THAN 250 (but see what happens at 25)
};
Response<Completions> completionsResponse = client.GetCompletions(completionsOptions);
Console.WriteLine($"\nPROMPT: \n\n{prompt}");
int i = 0;
foreach (var choice in completionsResponse.Value.Choices)
{
Console.WriteLine($"\nRESPONSE {++i}/{completionsResponse.Value.Choices.Count}:" +
$"{choice.Text}");
}
Console.WriteLine($"\n----------------- DEBUG INFO -----------------");
Console.WriteLine($"Tokens used: {completionsResponse.Value.Usage.CompletionTokens}/{completionsOptions.MaxTokens}");
Console.WriteLine("----------------------------------------------");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment