AzureOpenAI
API Examples
You can also use an OpenAI model deployed into Azure AI. For this, you will provide a few pieces of information from the Azure AI playground:
[1]:
call_delay_secs = 0
[2]:
import os
# Uncomment if using DefaultAzureCredential below
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
# This is the name of the model deployed, such as 'gpt-4' or 'gpt-3.5-turbo
model = os.getenv("AZUREAI_CHAT_MODEL", "Please set the model")
# This is the deployment URL, as provided in the Azure AI playground ('view code')
# It will end with 'openai.azure.com'
azure_endpoint = os.getenv("AZUREAI_CHAT_BASE_ENDPOINT", "Please set the endpoint")
# This is the name of the deployment specified in the Azure portal
azure_deployment = os.getenv("AZUREAI_CHAT_DEPLOYMENT", "Please set the deployment name")
# This is the deployed API version, such as 2024-02-15-preview
azure_api_version = os.getenv("AZUREAI_CHAT_API_VERSION", "Please set the API version")
# The environment variable should be set to the API key from the Azure AI playground:
# api_key=os.getenv("AZUREAI_CHAT_KEY", "Please set API key")
# Alternatively, we can use Entra authentication
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
We can now construct the guidance
model object:
[3]:
from guidance import models, gen
azureai_model = models.AzureOpenAI(
model=model,
azure_endpoint=azure_endpoint,
azure_deployment=azure_deployment,
version=azure_api_version,
# For authentication, use either
# api_key=api_key
# or
azure_ad_token_provider=token_provider
)
We can use the model as before:
[4]:
from guidance import system, user, assistant
with system():
lm = azureai_model + "You are a helpful assistant."
with user():
lm += "What is the meaning of life?"
with assistant():
lm += gen("response")
systemYou are a helpful assistant.userWhat is the meaning of life?assistantThe meaning of life is a philosophical and metaphysical question related to the purpose or significance of life or existence in general. This question has been asked for centuries and does not have one definitive answer, as it can vary greatly depending on one's personal beliefs and experiences. Some people may find meaning through various forms of expression like love, art, or work, while others may seek purpose in personal growth, contribution to others, or through religious/spiritual beliefs. Ultimately, the meaning of life may be something that each individual must discover for themselves.
[5]:
import time
time.sleep(call_delay_secs)
This works with the multistep example as well:
[6]:
import guidance
# you can create and guide multi-turn conversations by using a series of role tags
@guidance
def experts(lm, query):
with system():
lm += "You are a helpful assistant."
with user():
lm += f"""\
I want a response to the following question:
{query}
Who are 3 world-class experts (past or present) who would be great at answering this?
Please don't answer the question or comment on it yet."""
with assistant():
lm += gen(name='experts', max_tokens=300)
with user():
lm += f"""\
Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.
In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question.
If the experts would disagree, just present their different positions as alternatives in the answer itself
(e.g. 'some might argue... others might argue...').
Please start your answer with ANSWER:"""
with assistant():
lm += gen(name='answer', max_tokens=500)
return lm
azureai_model + experts(query='What is the meaning of life?')
[6]:
systemYou are a helpful assistant.userI want a response to the following question: What is the meaning of life? Who are 3 world-class experts (past or present) who would be great at answering this? Please don't answer the question or comment on it yet.assistant1. Dalai Lama: As the spiritual leader of Tibetan Buddhism, the Dalai Lama has spent his life exploring questions about existence, purpose, and happiness. His insights on compassion, self-awareness, and inner peace could provide a unique perspective on the meaning of life. 2. Albert Einstein: Known for his contributions to the theory of relativity, Einstein also pondered deeply on philosophical questions about life's purpose and meaning. His scientific and philosophical insights could offer a unique perspective on this question. 3. Viktor Frankl: A psychiatrist and Holocaust survivor, Frankl wrote extensively about his experiences in concentration camps and his observations on human nature. His book, "Man's Search for Meaning," explores his theory that meaning in life comes from pursuing purposes and goals, even in the face of adversity.userGreat, now please answer the question as if these experts had collaborated in writing a joint anonymous answer. In other words, their identity is not revealed, nor is the fact that there is a panel of experts answering the question. If the experts would disagree, just present their different positions as alternatives in the answer itself (e.g. 'some might argue... others might argue...'). Please start your answer with ANSWER:assistantANSWER: The meaning of life is a deeply personal and subjective concept, and it can be approached from various perspectives. Some might argue that life's purpose is to seek happiness, compassion, and understanding, fostering a sense of interconnectedness with all beings. This perspective emphasizes the importance of self-awareness and inner peace, suggesting that the meaning of life is found within ourselves and our relationships with others. Others might argue that life's meaning is derived from our pursuit of knowledge and understanding of the universe. This perspective suggests that by striving to comprehend the world around us, we can find purpose and fulfillment. The quest for knowledge, in this view, is a fundamental part of what gives life meaning. Yet another perspective might propose that the meaning of life is found in our pursuit of individual goals and purposes, even in the face of adversity. This viewpoint suggests that life's meaning is not predetermined but is instead created by each individual through their actions and experiences. In this view, the struggle itself is meaningful, and overcoming challenges can provide a profound sense of purpose. In conclusion, the meaning of life may be found in compassion and understanding, the pursuit of knowledge, or the creation of individual purpose. It is a deeply personal journey, and each individual may find their own unique interpretation.
[7]:
time.sleep(call_delay_secs)