DEV Community

Ravi
Ravi

Posted on

Sample python script: using Text-Bison model via Azure OpenAI

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

  1. Azure Account: Create an Azure account if you don’t have one.
  2. Create an OpenAI Resource: Set up an OpenAI resource in the Azure portal and obtain your endpoint and API key.
  3. Install Required Libraries:
   pip install requests
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Explanation

  1. Configuration: Replace <your-endpoint> and <your-api-key> with your Azure OpenAI endpoint and API key. The deployment_name should match the name of your Text-Bison model deployment.

  2. 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 and temperature.
  3. 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.
  4. 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 and temperature based on your requirements.
  • Make sure you handle any API limits or quotas as specified by Azure.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs