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

Highlight logprob with ChatInterface #121

Open
ahuang11 opened this issue Jan 16, 2024 · 0 comments
Open

Highlight logprob with ChatInterface #121

ahuang11 opened this issue Jan 16, 2024 · 0 comments

Comments

@ahuang11
Copy link
Collaborator

ahuang11 commented Jan 16, 2024

Based on https://cookbook.openai.com/examples/using_logprobs

Log probabilities of output tokens indicate the likelihood of each token occurring in the sequence given the context. To simplify, a logprob is log(p), where p = probability of a token occurring at a specific position based on the previous tokens in the context. Some key points about logprobs

Now, with logprobs enabled, we can see exactly how confident the model is in its predictions, which is crucial for creating an accurate and trustworthy classifier.

Lighter colors indicate lower probability, darker colors / black indicate higher probability

import panel as pn
import numpy as np
from math import exp
from openai import AsyncOpenAI

pn.extension()

COLORS = [
    '#94c4df',
    '#1764ab',
    '#084a92',
    '#08306b',
    '#000000'
]

def highlight(text, log_prob):
    linear_prob = np.round(exp(log_prob) * 100, 2)
    if linear_prob >= 0 and linear_prob < 20:
        color_idx = 0
    elif linear_prob >= 20 and linear_prob < 40:
        color_idx = 1
    elif linear_prob >= 40 and linear_prob < 60:
        color_idx = 2
    elif linear_prob >= 60 and linear_prob < 80:
        color_idx = 3
    else:
        color_idx = 4

    # Generate HTML output with the chosen color
    if "'" in text:
        html_output = f'<span style="color: {COLORS[color_idx]};">{text}</span>'
    else:
        html_output = f"<span style='color: {COLORS[color_idx]}'>{text}</span>"
    return html_output


async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    messages.append({"role": "user", "content": contents})
    response = await aclient.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        stream=True,
        logprobs=True,
        temperature=1.9
    )
    message = ""
    async for chunk in response:
        part = chunk.choices[0].delta.content
        token = chunk.choices[0].logprobs
        if part and token:
            log_prob = token.content[0].logprob
            message += highlight(part, log_prob)
            yield message
    messages.append({"role": "user", "content": message})

messages = []
aclient = AsyncOpenAI()
chat_interface = pn.chat.ChatInterface(callback=callback, callback_user="ChatGPT", callback_exception="verbose")
chat_interface.show()

Default temperature

openai_default_temp.mp4

Temperature = 1.9

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

No branches or pull requests

1 participant