from openai import OpenAI
from math import exp
import numpy as np
from IPython.display import display, HTML
import os

open_ai_key = "sk-None-BcvcTP9SBTw3yuuBzCE9T3BlbkFJq0ZmKf4zLrW2tpGZKAn5"
#open_ai_key = "sk-proj-nfLesDiu6oVaARYzTSOBT3BlbkFJpW0Y8kdWyLMhQKyx86Ip"
os.environ["OPENAI_API_KEY"] = open_ai_key 

client = OpenAI()


def get_completion(
    messages: list[dict[str, str]],
    model: str = "gpt-4o-mini",
    max_tokens=500,
    temperature=0,
    stop=None,
    seed=123,
    tools=None,
    logprobs=None,  # whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message..
    top_logprobs=None,
) -> str:
    params = {
        "model": model,
        "messages": messages,
        "max_tokens": max_tokens,
        "temperature": temperature,
        "stop": stop,
        "seed": seed,
        "logprobs": logprobs,
        "top_logprobs": top_logprobs,
    }
    if tools:
        params["tools"] = tools

    completion = client.chat.completions.create(**params)
    return completion


CLASSIFICATION_PROMPT = """You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: {headline}"""


headlines = [
    "Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.",
    "Local Mayor Launches Initiative to Enhance Urban Public Transport.",
    "Tennis Champion Showcases Hidden Talents in Symphony Orchestra Debut",
]


for headline in headlines:
    print(f"\nHeadline: {headline}")
    API_RESPONSE = get_completion(
        [{"role": "user", "content": CLASSIFICATION_PROMPT.format(headline=headline)}],
        model="gpt-4",
    )
    print(f"Category: {API_RESPONSE.choices[0].message.content}\n")


Headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.
Category: Technology, as it discusses a new smartphone.


Headline: Local Mayor Launches Initiative to Enhance Urban Public Transport.
Category: Politics, discusses a governmental initiative.


Headline: Tennis Champion Showcases Hidden Talents in Symphony Orchestra Debut
Category: Art, involves a symphony orchestra.
for headline in headlines:
    print(f"\nHeadline: {headline}")
    API_RESPONSE = get_completion(
        [{"role": "user", "content": CLASSIFICATION_PROMPT.format(headline=headline)}],
        model="gpt-4",
        logprobs=True,
        top_logprobs=1,
    )
    top_two_logprobs = API_RESPONSE.choices[0].logprobs.content[0].top_logprobs
    html_content = ""
    for i, logprob in enumerate(top_two_logprobs, start=1):
        html_content += (
            f"<span style='color: cyan'>Output token {i}:</span> {logprob.token}, "
            f"<span style='color: darkorange'>logprobs:</span> {logprob.logprob}, "
            f"<span style='color: magenta'>linear probability:</span> {np.round(np.exp(logprob.logprob)*100,2)}%<br>"
        )
    display(HTML(html_content))
    print("\n")

Headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.
Output token 1: Explanation, logprobs: -0.93188596, linear probability: 39.38%



Headline: Local Mayor Launches Initiative to Enhance Urban Public Transport.
Output token 1: Politics, logprobs: -0.056269404, linear probability: 94.53%



Headline: Tennis Champion Showcases Hidden Talents in Symphony Orchestra Debut
Output token 1: Art, logprobs: -0.21430759, linear probability: 80.71%

for headline in headlines:
    print(f"\nHeadline: {headline}")
    API_RESPONSE = get_completion(
        [{"role": "user", "content": CLASSIFICATION_PROMPT.format(headline=headline)}],
        model="gpt-4",
        logprobs=True,
        top_logprobs=3,
    )
    top_two_logprobs = API_RESPONSE.choices[0].logprobs.content[0].top_logprobs
    html_content = ""
    for i, logprob in enumerate(top_two_logprobs, start=1):
        html_content += (
            f"<span style='color: cyan'>Output token {i}:</span> {logprob.token}, "
            f"<span style='color: darkorange'>logprobs:</span> {logprob.logprob}, "
            f"<span style='color: magenta'>linear probability:</span> {np.round(np.exp(logprob.logprob)*100,2)}%<br>"
        )
    display(HTML(html_content))
    print("\n")

Headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.
Output token 1: a, logprobs: -0.8605204, linear probability: 42.29%
Output token 2: Technology, logprobs: -1.3646936, linear probability: 25.55%
Output token 3: A, logprobs: -2.1214533, linear probability: 11.99%



Headline: Local Mayor Launches Initiative to Enhance Urban Public Transport.
Output token 1: Politics, logprobs: -0.078849025, linear probability: 92.42%
Output token 2: a, logprobs: -3.2040284, linear probability: 4.06%
Output token 3: Explanation, logprobs: -3.4173195, linear probability: 3.28%



Headline: Tennis Champion Showcases Hidden Talents in Symphony Orchestra Debut
Output token 1: Art, logprobs: -0.03863497, linear probability: 96.21%
Output token 2: Sports, logprobs: -3.3875844, linear probability: 3.38%
Output token 3: A, logprobs: -5.7630534, linear probability: 0.31%

API_RESPONSE = get_completion(
    [{"role": "user", "content": CLASSIFICATION_PROMPT.format(headline=headlines[0])}],
    model="gpt-4",
    logprobs=True,
    top_logprobs=3,
)
message = {"role": "assistant", "content": ""}
message = CLASSIFICATION_PROMPT.format(headline=headlines[0])
for x in range(10):
    API_RESPONSE = get_completion(
        [{"role": "user", "content": message}],
        model="gpt-4o-mini",
        logprobs=True,
        top_logprobs=1,
    )
    message += API_RESPONSE.choices[0].logprobs.content[0].top_logprobs[0].token 
    print(message)
    for item in API_RESPONSE.choices[0].logprobs.content[0].top_logprobs:
        print(item.token, np.round(np.exp(item .logprob)*100,2))
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.a
a 99.33
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aa
a 98.84
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaa
a 99.2
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaa
a 99.12
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaaa
a 99.39
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaaaa
a 99.14
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaaaaa
a 98.65
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaaaaaa
a 98.73
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaaaaaaa
a 98.86
You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
MAKE SURE your output is one of the four categories stated. GIve a short explanation (at most a 5 word sentence). Start with the explanation and an 'a'
Article headline: Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.aaaaaaaaaa
a 97.59
from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
    messages=[{
        "role": "user",
        "content": "Give a fictional description of a movie in three words.",
    }],
    model="gpt-4o-mini",
    logprobs=True,
    temperature=0.2,
    #best_of=10
)

print(response.choices[0])

cum = 0  
for item in response.choices[0].logprobs.content:
    print(item.token, np.round(np.exp(item.logprob)*100,2) )
    cum += item.logprob

print('cum', np.round(np.exp(cum)*100,8))
Choice(finish_reason='stop', index=0, logprobs=ChoiceLogprobs(content=[ChatCompletionTokenLogprob(token='"', bytes=[34], logprob=-0.7496403, top_logprobs=[]), ChatCompletionTokenLogprob(token='Time', bytes=[84, 105, 109, 101], logprob=-0.04794023, top_logprobs=[]), ChatCompletionTokenLogprob(token='-tr', bytes=[45, 116, 114], logprob=-0.45240924, top_logprobs=[]), ChatCompletionTokenLogprob(token='avel', bytes=[97, 118, 101, 108], logprob=-0.00016480287, top_logprobs=[]), ChatCompletionTokenLogprob(token='ing', bytes=[105, 110, 103], logprob=-2.1531068e-05, top_logprobs=[]), ChatCompletionTokenLogprob(token=' love', bytes=[32, 108, 111, 118, 101], logprob=-0.9172403, top_logprobs=[]), ChatCompletionTokenLogprob(token=' triangle', bytes=[32, 116, 114, 105, 97, 110, 103, 108, 101], logprob=-0.83480483, top_logprobs=[]), ChatCompletionTokenLogprob(token='."', bytes=[46, 34], logprob=-0.00013548243, top_logprobs=[])], refusal=None), message=ChatCompletionMessage(content='"Time-traveling love triangle."', role='assistant', function_call=None, tool_calls=None, refusal=None))
" 47.25
Time 95.32
-tr 63.61
avel 99.98
ing 100.0
 love 39.96
 triangle 43.4
." 99.99
cum 4.96698725
I 58.33
 appreciate 26.26
 humans 52.3
. 99.78



I 50.12
 don't 61.71
 feel 53.12
. 99.99
cum 16.42623173
import numpy as np
cum = 0  
for item in response.choices[0].logprobs.content:
    print(item.token, np.round(np.exp(item.logprob)*100,2) )
    cum += item.logprob

print('cum', np.round(np.exp(cum)*100,8))
I 54.21
 appreciate 38.8
 humanity 42.7
. 99.75
cum 8.95858825
import numpy as np
cum = 0  
for item in response.choices[0].logprobs.content:
    print(item.token, np.round(np.exp(item.logprob)*100,2) )
    cum += item.logprob

print('cum', np.round(np.exp(cum)*100,8))
import networkx as nx
import plotly.graph_objects as go
import numpy as np

# Sample data: Each path is represented by a list of (token, likelihood) tuples
# These are example sequences; replace these with your actual sampled sequences and probabilities.
sampled_sequences = [
    [("The", 0.9), ("quick", 0.8), ("brown", 0.7)],
    [("The", 0.9), ("slow", 0.6), ("turtle", 0.5)],
    [("A", 0.85), ("quick", 0.75), ("fox", 0.65)],
    [("A", 0.85), ("slow", 0.6), ("brown", 0.4)]
]

# Initialize the graph
G = nx.DiGraph()

# Add nodes and edges to the graph
for sequence in sampled_sequences:
    current_path = ""
    for i, (token, likelihood) in enumerate(sequence):
        node_label = f"{token} ({likelihood})"
        
        # Update path and add node with hover text containing likelihood and sentence
        current_path += f" {token}"
        hover_text = f"Token: {token}<br>Likelihood: {likelihood}<br>Path: {current_path.strip()}"
        
        if i == 0:  # Add the starting node
            G.add_node(node_label, hover=hover_text, path=current_path, likelihood=likelihood, pos=(i, len(G.nodes)))
        else:  # Add the edge from previous to current token
            prev_node_label = f"{sequence[i-1][0]} ({sequence[i-1][1]})"
            G.add_node(node_label, hover=hover_text, path=current_path, likelihood=likelihood, pos=(i, len(G.nodes)))
            G.add_edge(prev_node_label, node_label, weight=np.log(likelihood))

# Set up left-to-right positions for nodes using their "pos" attribute
pos = {node: (data["pos"][0], data["pos"][1]) for node, data in G.nodes(data=True)}

# Plotting with Plotly
edge_x = []
edge_y = []
for edge in G.edges():
    x0, y0 = pos[edge[0]]
    x1, y1 = pos[edge[1]]
    edge_x.extend([x0, x1, None])
    edge_y.extend([y0, y1, None])

edge_trace = go.Scatter(
    x=edge_x, y=edge_y,
    line=dict(width=0.5, color='#888'),
    hoverinfo='none',
    mode='lines'
)

# Create node traces with hover text for each node
node_x = []
node_y = []
node_text = []
for node in G.nodes():
    x, y = pos[node]
    node_x.append(x)
    node_y.append(y)
    node_text.append(G.nodes[node]['hover'])

node_trace = go.Scatter(
    x=node_x, y=node_y,
    mode='markers+text',
    text=[node.split(" ")[0] for node in G.nodes()],  # Show token only (without likelihood) for cleanliness
    textposition="bottom center",
    marker=dict(size=10, color='#FFA07A'),
    hoverinfo='text',
    hovertext=node_text
)

# Assemble the figure with a left-to-right layout
fig = go.Figure(data=[edge_trace, node_trace],
                layout=go.Layout(
                    title='Sampled Sequences Visualization (Left-to-Right)',
                    showlegend=False,
                    hovermode='closest',
                    margin=dict(b=0, l=0, r=0, t=50),
                    xaxis=dict(showgrid=False, zeroline=False, visible=False),
                    yaxis=dict(showgrid=False, zeroline=False, visible=False)
                ))

fig.show()
Unable to display output for mime type(s): application/vnd.plotly.v1+json