Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to go about explaining or interpreting text for chat completion models? #235

Open
karrtikiyer-tw opened this issue Feb 27, 2024 · 1 comment

Comments

@karrtikiyer-tw
Copy link

Hi community,
Any thoughts on how we can get some of the lime tool explainers to work on the Open AI Chat Completion models?
Any advise or help appreciated.
Thanks,
Karrtik

@Siddharth-Latthe-07
Copy link

@karrtikiyer-tw Interesting topic,
here are some of the introductory steps that will help you to get an idea regarding the above,

  1. install the required libraries (lime)
  2. define a wrapper function:-
    As LIME requires a prediction function that returns a probability distribution over classes and Since Chat Completion models provide text, you need to define a function that translates the text output into a format suitable for LIME.
import openai

def openai_completion(prompt, model="gpt-4", max_tokens=50):
    response = openai.Completion.create(
        engine=model,
        prompt=prompt,
        max_tokens=max_tokens,
        n=1,
        stop=None,
        temperature=0.7
    )
    return response.choices[0].text.strip()
  1. create function for lime explainer:-
    For text models, it involves altering the input text slightly and observing the resulting completions.
from lime.lime_text import LimeTextExplainer
import numpy as np

class OpenAIWrapper:
    def __init__(self, model="gpt-4"):
        self.model = model
    
    def predict_proba(self, texts):
        # Define the class labels
        classes = ['positive', 'negative']
        
        # Simulate probabilities (for demonstration)
        # In practice, you'd replace this with a real probability scoring
        probas = []
        for text in texts:
            completion = openai_completion(text, model=self.model)
            # Dummy scoring: length-based probability (you'll need a real classifier)
            probas.append([len(completion) % 2, (len(completion) + 1) % 2])
        return np.array(probas)

# Initialize LIME explainer and the OpenAI model wrapper
explainer = LimeTextExplainer(class_names=['positive', 'negative'])
model_wrapper = OpenAIWrapper(model="gpt-4")

# Example text input
text = "The weather today is"

# Generate explanation
exp = explainer.explain_instance(text, model_wrapper.predict_proba, num_features=10)

# Print the explanation
print(exp.as_list())

and finally, interpreting the results, through the explain_instance function
Hope, this would give you a brief idea, open for your thoughts on this
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants