Getting Began
Goal
Discover ways to work together with OpenAI’s fashions to generate textual content completions utilizing a unique context. On this lab, you’ll use the Azure OpenAI consumer to get responses for prompts about universities, then discover completely different prompts and fashions.
Step 1: Set Up Your Setting
Earlier than beginning, guarantee you may have the required packages put in and imported. Set up the openai
bundle and import vital libraries.
!pip set up openai==1.13.3 -qqqfrom openai import AzureOpenAI
import os
from datetime import datetime
Step 2: Initialize the Azure OpenAI Shopper
You have to arrange your Azure OpenAI consumer. Substitute 'your_endpoint'
and 'your_api_key'
along with your precise Azure OpenAI endpoint and API key.
consumer = AzureOpenAI(
azure_endpoint = "your_endpoint", # Substitute along with your Azure OpenAI endpoint
api_key = "your_api_key", # Substitute along with your API key
api_version = "2024-02-15-preview"
)
Step 3: Create and Format Your Immediate
On this step, you’ll create a immediate asking for details about “Stanford College” and “California State College, Sacramento”.
message = [
{"role": "system", "content": "You are an AI assistant that helps people find information."},
{"role": "user", "content": "Please tell me about Stanford University and California State University, Sacramento."}
]response = consumer.chat.completions.create(
mannequin="DrLeeTTAI2333", # mannequin = "deployment_name"
messages = message,
temperature=0.7,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
cease=None
)
Step 4: Ship the Immediate to the Mannequin
Now, ship the immediate to the mannequin and get the completion. Modify the mannequin identify as vital.
response = consumer.chat.completions.create(
mannequin="DrLeeTTAI2333", # mannequin = "deployment_name"
messages = message,
temperature=0.7,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
cease=None
)
Step 5: Print the Response
Create a perform to format and print the response properly.
from datetime import datetime # Make certain to incorporate this importdef print_formatted_response(completion):
# Print a header to separate every response
print("n--- Response Particulars ---")
# Loop by way of every selection within the completion (usually there shall be only one)
for selection in completion.selections:
# Print the response content material
print("nContent:")
print(selection.message.content material)
# Print the end motive
print("nFinish Motive:", selection.finish_reason)
# Print the function of the message
print("nRole:", selection.message.function)
# Print device calls and performance name if relevant
print("nFunction Name:", selection.message.function_call)
if selection.message.tool_calls:
print("nTool Calls:")
for tool_call in selection.message.tool_calls:
print(tool_call)
# Print content material filter outcomes
print("nContent Filter Outcomes:")
for key, worth in selection.content_filter_results.objects():
print(f"{key.capitalize()}: Filtered={worth['filtered']}, Severity={worth['severity']}")
# Print token utilization data
print("nTokens Used:", completion.utilization.total_tokens)
# Print the mannequin used
print("nModel Used:", completion.mannequin)
# Print the creation time
print("nCreated At:", completion.created)
# Convert Unix timestamp to human-readable date and time
created_at = datetime.utcfromtimestamp(completion.created).strftime('%Y-%m-%d %H:%M:%S UTC')
print("nCreated At:", created_at)
# Print formatted response
print_formatted_response(response)
Step 6: Discover with Completely different Prompts and Fashions
Now, encourage college students to experiment with completely different prompts and fashions. Listed here are some ideas:
- Strive altering the immediate to ask about completely different universities or different varieties of data.
- Experiment with completely different fashions like
gpt-4
or different variations. - Modify parameters like
temperature
,max_tokens
, andtop_p
to see how they have an effect on the output.
# Instance of a unique immediate
new_message = [
{"role": "system", "content": "You are an AI assistant."},
{"role": "user", "content": "How can AI impact education over the next decade?"}
]# Ship the brand new immediate to the mannequin
new_response = consumer.chat.completions.create(
mannequin = "gpt-4", # Strive with gpt-4 and ensure it's deployed!!!
messages = new_message,
temperature = 0.5,
max_tokens = 1000,
top_p = 0.9,
frequency_penalty = 0,
presence_penalty = 0.2,
cease = ["n", " Human:", " AI:"]
)
print_formatted_response(new_response)
This lab information gives a primary construction for interacting with OpenAI’s fashions utilizing the Azure consumer. By following these steps, college students will learn to ship prompts to the mannequin, obtain responses, and discover completely different settings and fashions to know the flexibleness and energy of AI in processing pure language. Modify the lab information in line with your particular course and infrastructure setup.