Link to chat (with first-message embedded)

For those working on Notebooks / JupyterLab / Colab.

You probably know Jupyter-ai [1], which you can use to query an LLM with whatever is on the cell.

%%ai
Write a poem about C++.

But sometimes you may want to talk/chat about it, not just a single response. Or work on in from Canvas. Or plot it, twist it, explode it.

With this simple function.

import urllib.parse
from IPython.display import Markdown

def chat_link(text: str, title: str ="Chat Link"):
    """
    Given a text and a title, returns a markdown link 
    to ChatGPT with the `text` already inserted in the chat.
    """

    url = "https://chat.openai.com"

    # convert the text to urlencoded
    text = urllib.parse.quote(text)

    url_complete= f"{url}/?q={text}"

    # create the markdown-link
    markdown_format=f"[{title}]({url_complete})"

    # Display as markdown
    return Markdown(markdown_format)
# ------------------

You can put all the data* you want in a text-variable, and do

chat_link(x)

Click on it, and your text is on the Web-Chat interface.

If you are not on Notebooks or alike, I am sure you can modify the code to suit your needs.

* Probably there is a limit, if you know let me know to say it here.

[1] Jupyter AI — Jupyter AI documentation