I want a unified interface with Open AI’s API and Hugging Face models to have a consistent interface depending on what model I am using.
Does this exist? Wanted to check tested version before I wrote my own but here are some possibilities:
import openai
import torch
from transformers import BertTokenizer, BertForSequenceClassification
class OpenAI_HF_Interface:
def __init__(self, openai_key, hf_model_name):
openai.api_key = openai_key
self.hf_model = BertForSequenceClassification.from_pretrained(hf_model_name)
self.tokenizer = BertTokenizer.from_pretrained(hf_model_name)
def generate_text(self, prompt):
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=60
)
return response.choices[0].text.strip()
def classify_text(self, text):
inputs = self.tokenizer(text, return_tensors="pt")
outputs = self.hf_model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
return probabilities
# Test the class
interface = OpenAI_HF_Interface('your-openai-api-key', 'bert-base-uncased')
generated_text = interface.generate_text("Translate the following English text to French: '{}'")
print(generated_text)
probabilities = interface.classify_text(generated_text)
print(probabilities)
or
import openai
from transformers import pipeline
openai.api_key = "YOUR_OPENAI_KEY"
nlp = pipeline("sentiment-analysis")
class AIAssistant:
def generate_and_classify(self, prompt):
completions = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=100
)
generated_text = completions.choices[0].text.strip()
result = nlp(generated_text)
sentiment = result[0].get('label')
return generated_text, sentiment
assistant = AIAssistant()
text, sentiment = assistant.generate_and_classify("How good was the movie yesterday?")
print(text)
print(sentiment)
prompt = "How good was the movie yesterday?"
text, sentiment = assistant.generate_and_classify(prompt)
print(text)
# "The movie yesterday was absolutely incredible! The acting, writing, and cinematography were all top-notch. It was gripping from start to finish and really moved me emotionally. Definitely one of the best films I've seen in the past year."
print(sentiment)
# 'POSITIVE'
note it’s not been tested.
Also avoiding lang chains due to bad reputation e.g.,