Using OCI Generative AI to categorise email feedback and send responses

OCI Generative AI is a fully managed Oracle Cloud Infrastructure service that provides customizable large language models (LLMs) that cover a wide range of use cases, including chat, text generation, summarization, and creating text embeddings.

You can use APIs & scripts to embed OCI Gen AI to your applications after testing it out on the playground from the OCI console.

In the example below using python.

# coding: utf-8
# Copyright (c) 2023, Oracle and/or its affiliates.  All rights reserved.
# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.

##########################################################################
# chat_demo.py
# Supports Python 3
##########################################################################
# Info:
# Get texts from LLM model for given prompts using OCI Generative AI Service.
##########################################################################
# Application Command line(no parameter needed)
# python chat_demo.py
##########################################################################
import oci

# Setup basic variables
# Auth Config
# TODO: Please update config profile name and use the compartmentId that has policies grant permissions for using Generative AI Service
compartment_id = "ocid1.compartment.oc1.."
CONFIG_PROFILE = "DEFAULT"
config = oci.config.from_file('~/.oci/config', CONFIG_PROFILE)

# Service endpoint
endpoint = "https://inference.generativeai...."

generative_ai_inference_client = oci.generative_ai_inference.GenerativeAiInferenceClient(config=config, service_endpoint=endpoint, retry_strategy=oci.retry.NoneRetryStrategy(), timeout=(10,240))
chat_detail = oci.generative_ai_inference.models.ChatDetails()

chat_request = oci.generative_ai_inference.models.CohereChatRequest()
#chat_request.message = "{input}"

chat_request.message = """

Instructions:
Here are customer feedback emails for a book retailer. Please 
classify each of the emails in 2 categories: good feedback and bad feedback, don't give an explanation.
Write a response for each of the good & bad feedback emails. Be polite and proffesional. 

Data:

Email 1: 
Subject: A Bookworm's Paradise!

Dear BookHunter ,

Just wanted to drop a note and say how thrilled I am with your diverse collection of books. Every visit feels like an adventure, and I always find hidden gems that aren't available elsewhere. Keep up the fantastic work!

 

Email 2: 
Subject: Shipping Delays - A Small Wrinkle in My Experience

Hello BookHunter ,

I've been a loyal customer for years and have always loved your book selection. However, my last order took longer than expected to arrive. Perhaps there's an opportunity to review your shipping partners or provide clearer delivery estimates?


Email 3: 
Subject: Disheartened by My Recent Experience

Dear BookHunter ,

I hate to say this, but my recent visit to your store was quite disappointing. The staff seemed disinterested and didn't help when I asked for recommendations. Such a stark contrast from the warm, welcoming experience I used to love. I hope this was just a one-off.
"""

chat_request.max_tokens = 600
chat_request.temperature = 0.25
chat_request.frequency_penalty = 0
chat_request.top_p = 0.75
chat_request.top_k = 0


chat_detail.serving_mode = oci.generative_ai_inference.models.OnDemandServingMode(model_id="ocid1.generativeaimodel.oc1.uk-london-1.a....")
chat_detail.chat_request = chat_request
chat_detail.compartment_id = compartment_id
chat_response = generative_ai_inference_client.chat(chat_detail)
# Print result
print("**************************Chat Result**************************")
print(vars(chat_response))

Results:

Happy Coding!

Installing OCI CLI on Mac OS

Command Line Interface (CLI) provides the same core functionality as the console, plus additional commands, like the ability to run scripts which extends the console functionality.

To install CLI, you must have

  • OCI cloud account
  • User in a group with the right permissions
  • A key pair used for signing API requests- public key uploaded at Oracle cloud

To install the CLI on Mac OS with HomeBrew:

brew update && brew install oci-cli

To verify the OCI CLI installation

oci --version

To set up the configuration File- there are two ways to do it;

a) Use the set up config Dialog

oci setup config

this command prompts with the information required to create the configuration file and API keys. After the set up, you should upload the public api key on your the API keys section of your profile on OCI.

b) Manual set up

Log into your Oracle cloud, go to your profile, -> API keys & add an API key

Add an API Key in PEM format or generate an API key pair

Download the private key & save config file details – You will use them to set up the config file.

On your MAC OS, navigate to the .oci folder.

cd ~/.oci

if you don’t have the .oci folder, make the directory

mkdir ~/.oci 

Paste the private key downloaded from OCI on this folder and edit the permissions to ensure that only you can read the private key file:

 chmod go-rwx ~/.oci/<oci_api_key_name.pem  

Create a config file and paste the config file details from OCI.

vi config

The config file content looks similar to this- add yours,

[DEFAULT]
user=ocid1.user.oc1..<unique_ID>
fingerprint=<your_fingerprint>
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..<unique_ID>
region=uk-london-1

(To exit vi, press escape, :wq!)

All done! Test if the OCI CLI is correctly installed and working;

oci os ns get

You should be able to read, create, update, delete the OCI resources. The output from the above command should be the Tenancy name of your OCI cloud.

{
  "data": "<tenancyname>"
}

To create an OCI object storage bucket using OCI CLI example

oci os bucket create --compartment-id <Compartment OCID> --name TestCLI

Add a file on Object Storage using the pipe command

echo "Hello World" | oci os object put --bucket-name my-bucket --name file.txt --file - 

Delete Objects in a bucket (Use –dry-run at the end if you want to first preview)

oci os object bulk-delete --namespace-name <object_storage_namespace> --bucket-name <bucket_name> 

Other useful cmds;

To upgrade your CLI install on Mac OS using Homebrew:

brew update && brew upgrade oci-cli

To uninstall the CLI on Mac OS using Homebrew:

brew uninstall oci-cli

Creating OCI CLI profile

oci setup config --profile myprofile

To use the profile, specify the name on the –profile section

oci os ns get --profile myprofile

To generate new public and private keys on the .oci folder

openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem

References

Oracle Cloud Documentation