To use the Text-Bison model via Azure OpenAI, you need to set up your Azure account and configure the necessary resources. Below is a sample Python script that demonstrates how to interact with the Text-Bison model using Azure OpenAI.
Prerequisites
- Azure Account: Create an Azure account if you don’t have one.
- Create an OpenAI Resource: Set up an OpenAI resource in the Azure portal and obtain your endpoint and API key.
- Install Required Libraries:
pip install requests
Sample Python Script
Here's a basic script to interact with the Text-Bison model:
import requests
# Azure OpenAI configuration
endpoint = "https://<your-endpoint>.openai.azure.com/"
api_key = "<your-api-key>"
deployment_name = "text-bison" # Your deployment name
def generate_text(prompt):
url = f"{endpoint}openai/deployments/{deployment_name}/completions?api-version=2023-05-15"
headers = {
"Content-Type": "application/json",
"api-key": api_key
}
# Define the request body
data = {
"prompt": prompt,
"max_tokens": 100,
"temperature": 0.7
}
# Make the request to the Azure OpenAI API
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()['choices'][0]['text'].strip()
else:
print(f"Error: {response.status_code} - {response.text}")
return None
if __name__ == "__main__":
prompt = "What are the benefits of using AI in healthcare?"
generated_text = generate_text(prompt)
if generated_text:
print("Generated Text:", generated_text)
Explanation
Configuration: Replace
<your-endpoint>
and<your-api-key>
with your Azure OpenAI endpoint and API key. Thedeployment_name
should match the name of your Text-Bison model deployment.-
Function to Generate Text:
- The
generate_text
function constructs the API request. - It sets the necessary headers for authentication and specifies the request body, including the prompt and parameters like
max_tokens
andtemperature
.
- The
-
Making the Request:
- The script uses the
requests
library to send a POST request to the Azure OpenAI API. - If the request is successful, it returns the generated text; otherwise, it prints an error message.
- The script uses the
Execution: The script runs a prompt and prints the generated text.
Notes
- Ensure you have set the proper permissions and configurations in your Azure portal.
- Adjust parameters such as
max_tokens
andtemperature
based on your requirements. - Make sure you handle any API limits or quotas as specified by Azure.
Top comments (0)