OpenCloud.codes!

Generating, Explaining, and Transforming Text, Summarization, and Translation in NLP.

author image
Amit Puri

Advisor and Consultant

Posted on 02-Aug-2023, 13 min(s) read

Dive into the core functionalities of modern Natural Language Processing (NLP) with a focus on text completion and text-to-text transformations. From generating creative content to summarizing vast documents, discover how advanced machine learning models are reshaping our textual interactions and bridging communication gaps.

Text completion and text-to-text transformations are two pivotal facets of modern Natural Language Processing (NLP).

Text Completion: As the name suggests, text completion involves predicting and generating coherent and contextually relevant continuations for a given piece of text. This often leverages sophisticated models to anticipate user intent or complete sentences in a meaningful manner. Examples of its applications include: - Explaining a concept, topic, or code. - Generating ideas, questions, quizzes, emails, product descriptions, job posts, prompts, titles, and taglines. - Crafting jokes, stories, poems, dialogue scripts, or science fiction. - Organizing instructions or ideas in a paragraph as bullet points.

Text-to-Text Transformations: This encompasses a broader range of tasks where the objective is to convert or adapt a given text into another form while preserving its essence. This includes: - Summarization: Distilling lengthy articles or documents into concise summaries. - Rewriting: Correcting grammar or adapting writing style. - Extraction: Pulling specific content or entities from a larger body of content. - Reasoning & Classification: Engaging in chain-of-thought reasoning, classifying text, detecting intent, and analyzing sentiment. - Translation: Converting text from one language to another. - Paraphrasing: Rephrasing information to convey the same meaning in a different manner.

Powered by advanced machine learning models, these capabilities are revolutionizing the way we interact with and process textual information. They enhance communication efficiency and bridge linguistic and informational gaps.

Navigating the world of artificial intelligence requires a deep understanding of various models and platforms, each offering unique capabilities.

In this walkthrough, we'll embark on a comprehensive journey through some of the most prominent AI models or APIs available today: - OpenAI GPT-4, GPT-3.5 models via OpenAI Python Library, - LLaMA-2 models via TogetherAPI, - Bison and Gecko models via Google's Vertex AI PaLM API.

Specifically tailored for Python enthusiasts, this guide aims to demystify the process of integrating and leveraging these platforms, providing clear steps and insights to harness their full potential. Whether you're a seasoned developer or a curious beginner, this exploration promises to shed light on the practicalities of implementing cutting-edge AI solutions in Python.

Work in progress from here....

1. Environment Setup:

from dotenv import load_dotenv
load_dotenv()

This code imports the load_dotenv function from the dotenv module and then calls it. The purpose of this is to load environment variables from a .env file located in the same directory as the script. This is a common practice to manage secrets or configuration settings without hardcoding them into the main script.

2. Importing Libraries:

import os
import openai

Here, the code imports two libraries: - os: Provides a way to use operating system dependent functionality, like reading environment variables. - openai: The official library to interact with the OpenAI API.

3. Model Selection:

#model = "gpt-35-turbo"
model = "gpt-4"

The code specifies which model to use for text completion. In this case, it's set to use "gpt-4". The line for "gpt-35-turbo" is commented out, indicating it's not in use.

4. Defining Prompts:

prompt: str = "Write an introductory paragraph to explain Generative AI..."
system_prompt: str = "Explain in detail to help student understand the concept."
assistant_prompt: str = None

Here, three prompts are defined: - prompt: The main question or statement. - system_prompt: A directive to the model, guiding it on how to respond. - assistant_prompt: Another potential prompt for the model, but it's set to None, meaning it's not used.

5. Message Structure:

messages = [
    {"role": "user", "content": f"{prompt}"},
    {"role": "system", "content": f"{system_prompt}"},
    {"role": "assistant", "content": f"{assistant_prompt}"}
]

This code structures the prompts into a list of messages. Each message has a "role" (user, system, or assistant) and "content" (the actual text of the prompt).

6. API Configuration:

openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_version = '2020-11-07'

Here, the code sets up the OpenAI API: - The API key is retrieved from the environment variables using os.getenv. - The API version is set to '2020-11-07'.

7. Generating Text Completion:

completion = openai.ChatCompletion.create(
    model = model,
    messages = messages,
    temperature = 0.7
)

This code calls the ChatCompletion.create method from the openai library. It uses the previously defined model and messages to generate a text completion. The temperature parameter affects the randomness of the output; a lower value makes it more deterministic.

8. Displaying the Output:

print(completion)
response = completion["choices"][0]["message"].content
print(response)

Finally, the code prints the complete response from the API. It then extracts the actual generated text from the response and prints it.

In summary, this code sets up a connection to the OpenAI API, defines a prompt, and requests a text completion based on that prompt using the "gpt-4" model. The generated response is then printed to the console.

Text Completion using Azure OpenAI Service

Text completion is a powerful tool in the realm of Natural Language Processing (NLP). It aids in generating coherent and contextually relevant text based on a given prompt. With the advent of cloud services and machine learning platforms, leveraging these capabilities has become more accessible than ever. One such service is the Azure OpenAI Service. Let's delve into how text completion can be achieved using this service, as demonstrated in the provided code.

Setting Up the Environment

Before diving into the actual code, it's essential to set up the environment. The code uses the dotenv library to load environment variables. This is a common practice to keep sensitive information, such as API keys, separate from the main codebase.

from dotenv import load_dotenv
load_dotenv()

Importing Necessary Libraries

The code imports essential libraries like os for interacting with the operating system and openai for leveraging the OpenAI functionalities.

import os
import openai

Configuration and Model Selection

The code retrieves the Azure OpenAI endpoint and deployment name from the environment variables. It also specifies the model to be used for text completion. In this case, the model "gpt-4" is chosen.

azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
azure_deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
model = "gpt-4"

Defining the Prompt

The prompt is the initial text or question based on which the model will generate the completion. The code defines a user prompt, a system prompt, and an assistant prompt. These prompts guide the model in generating the desired output.

prompt: str = "Write an introductory paragraph to explain Generative AI..."
system_prompt: str = "Explain in detail to help student understand the concept."
assistant_prompt: str = None

API Configuration

The OpenAI API key, type, version, and base are set up using the environment variables and the Azure endpoint.

openai.api_key = os.getenv("AZURE_OPENAI_KEY")
openai.api_type = "azure"
openai.api_version = "2023-05-15"
openai.api_base = f"https://{azure_endpoint}.openai.azure.com"

Generating the Completion

The ChatCompletion.create method is used to generate the text completion. It takes in the model, engine (deployment name), messages (prompts), and a temperature parameter. The temperature affects the randomness of the output. A lower value makes the output more deterministic, while a higher value makes it more random.

completion = openai.ChatCompletion.create(
    model = model, 
    engine = azure_deployment_name,
    messages = messages,
    temperature = 0.7
)

Displaying the Output

Finally, the generated completion is printed out. The response is extracted from the completion object and displayed.

print(completion)
response = completion["choices"][0]["message"].content
print(response)

The Azure OpenAI Service offers a seamless way to integrate advanced text completion capabilities into applications. By leveraging cloud-based machine learning models like "gpt-4", developers can harness the power of AI to generate contextually relevant text based on user-defined prompts. Whether it's for chatbots, content generation, or any other application, the possibilities are vast and exciting.

Google's Vertex AI

Google's Vertex AI to generate text based on a given prompt using a specified model. Let's break it down step by step:

1. Environment Setup:

from dotenv import load_dotenv
load_dotenv()

This code imports the load_dotenv function from the dotenv module and then calls it. This function loads environment variables from a .env file in the same directory as the script, which is a common practice to manage secrets or configuration settings securely.

2. Importing Libraries:

import os
from google.oauth2 import service_account
import vertexai
from vertexai.language_models import TextGenerationModel

The code imports necessary libraries and modules: - os: Provides functionality to interact with the operating system. - service_account: From Google's OAuth2 library, it helps in authenticating using service account credentials. - vertexai: The main library to interact with Google's Vertex AI. - TextGenerationModel: A specific module from vertexai for text generation tasks.

3. Configuration:

google_project_id = os.getenv("GOOGLE_PROJECT_ID")
model:str = "text-bison@001"
location:str = "us-central1"
temperature:float = 0.7
prompt: str = "Write an introductory paragraph to explain Generative AI..."
parameters = {
        "temperature": temperature}

Here, the code sets up various configurations: - google_project_id: Retrieves the Google Cloud project ID from environment variables. - model: Specifies the model to be used for text generation. - location: Defines the Google Cloud region. - temperature: Sets the randomness of the generated output. - prompt: The main question or statement for the model. - parameters: A dictionary containing additional parameters for the model, in this case, just the temperature.

4. Authentication and Text Generation:

cred_file = 'gcp-cred.json'
if os.path.isfile(cred_file):
   credentials = service_account.Credentials.from_service_account_file(cred_file)
   vertexai.init(
    project=google_project_id,
    location = location,
    credentials = credentials)
   model = TextGenerationModel.from_pretrained(model)
   response = model.predict(prompt, **parameters)
   print(response.text)
else:
   print("Error: unable to find GCP Vertex AI credential file!")

This section does the following: - Checks if the gcp-cred.json file (which contains Google Cloud service account credentials) exists. - If it exists, the code: - Loads the credentials from the file. - Initializes the Vertex AI environment with the project ID, location, and credentials. - Loads the specified text generation model. - Generates text based on the given prompt and parameters. - Prints the generated text. - If the credentials file doesn't exist, it prints an error message.

In summary, this code sets up a connection to Google's Vertex AI, loads a specific text generation model, and generates text based on a given prompt. The generated response is then printed to the console.

Llama-2

How to use the Llama-2 model with the Together API to generate text based on a given prompt. Let's break it down step by step:

1. Importing Libraries:

import os
import together
from dotenv import load_dotenv
  • os: Provides functionality to interact with the operating system, especially useful for retrieving environment variables.
  • together: The main library to interact with the Together API.
  • dotenv: A module to load environment variables from a .env file.

2. Loading Environment Variables:

load_dotenv()

This code calls the load_dotenv function, which loads environment variables from a .env file located in the same directory as the script. This is a common practice to manage secrets or configuration settings securely.

3. Setting the Prompt:

prompt: str = "Write an introductory paragraph to explain Generative AI..."

Here, the code defines the prompt, which is the main question or statement that will be sent to the model for generating a response.

4. API Configuration:

together.api_key = os.getenv("TOGETHER_API_KEY")

The code retrieves the Together API key from the environment variables and assigns it to the together.api_key attribute. This key is essential for authenticating and interacting with the Together API.

5. Model Selection:

model: str = "togethercomputer/llama-2-70b-chat"

The code specifies the model to be used for text generation. In this case, it's set to use the "llama-2-70b-chat" model from Together.

6. Generating Text:

output = together.Complete.create(prompt, model=model, temperature=0.7)

This code calls the Complete.create method from the together library. It sends the previously defined prompt and model to the API, along with a temperature parameter. The temperature affects the randomness of the output; a lower value makes the output more deterministic, while a higher value makes it more random.

7. Extracting and Printing the Output:

text = output['output']['choices'][0]['text']
print(text)

The code extracts the generated text from the API's response and then prints it. The response is structured as a dictionary, and the actual generated text is nested within it.

In summary, this code sets up a connection to the Together API, specifies the Llama-2 model for text generation, sends a prompt to the model, and then prints the generated response to the console.

Here are the instructions on how to use this repo https://github.com/amitpuri/LLM-Text-Completion and details about setting up various environment variables and configurations for different AI platforms and services. Let's break it down:

1. Setting Environment Variables:

The text starts by mentioning that environment variables should be set in a .env file. This is a common practice to store configuration settings and secrets securely.

2. OpenAI Configuration:

  • OPENAI_API_KEY: This is the API key for OpenAI, and you can obtain it from the provided OpenAI platform link.

3. Azure OpenAI Service Configuration:

  • AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_DEPLOYMENT_NAME: These are settings related to the Azure OpenAI Service. The values for these can be found in the Azure portal at the provided link.

4. Google Palm AI Configuration:

  • GOOGLE_PALM_AI_API_KEY: This is the API key for Google's Palm AI service. It can be obtained from the provided MakerSuite link. *MakerSuite is now Google AI Studio

5. LLaMA-2 Together API Configuration:

  • TOGETHER_API_KEY: This is the API key for the LLaMA-2 model on the Together platform. You can get this key from the provided Together API playground link.

6. Vertex AI Configuration:

  • GOOGLE_PROJECT_ID: This is the project ID for Google's Vertex AI. Instructions are provided on how to obtain this ID:
  • Visit the Google Cloud console.
  • On the project selector page, either select an existing Google Cloud project or create a new one.
  • Ensure billing is enabled for the selected project.
  • Enable the Vertex AI API.

  • GCP Credential File: The text instructs users to add a Google Cloud Platform (GCP) credential file named gcp-cred.json for Vertex AI. This file contains authentication details for the service. Steps to obtain this file are provided:

  • Go to the IAM section in the Google Cloud console.
  • Navigate to Service accounts.
  • Select a service account.
  • Download the keys for the selected account. The link provided directs users to the service accounts section in the Google Cloud console.

In summary, this text provides a comprehensive guide on setting up environment variables and configurations for various AI services, including OpenAI, Azure OpenAI, Google Palm AI, LLaMA-2 Together API, and Google's Vertex AI.

References - Bring your own Data to Azure OpenAI: Step-by-Step Guide - Together API Docs - LiteLLM.ai Docs

Share on

Tags

Subscribe to see what we're thinking

Subscribe to get access to premium content or contact us if you have any questions.

Subscribe Now