Note-OpenAI

This application is a lightweight text editor, something like Notepad in Windows, but with web interface, and enhanced with AI-assisted features powered by OpenAI.

It integrates with OpenAI’s 🤖  Responses API to provide intelligent text operations.


🧩  OpenAI Models

https://developers.openai.com/api/docs/models

🏷  OpenAI Model Pricing

https://developers.openai.com/api/docs/pricing#latest-models


💬  Prompt Examples


🧭  Model guidance

https://developers.openai.com/api/docs/guides/latest-model

📈  Prompt Optimizer

https://platform.openai.com/chat/edit?optimize=true

🧠  Reasoning models

https://developers.openai.com/api/docs/guides/reasoning

🪶  Text generation

https://developers.openai.com/api/docs/guides/text

🪄  Prompt engineering

https://developers.openai.com/api/docs/guides/prompt-engineering

📚  OpenAI Cookbook

https://developers.openai.com/cookbook


🔢  Tiktoken in CodeWiki

https://codewiki.google/github.com/openai/tiktoken

📏  Tiktoken How-to Cookbook

https://developers.openai.com/cookbook/examples/how_to_count_tokens_with_tiktoken


☯  Literate Programming

Script is written in 👓  literate programming.


import streamlit as st
from openai import OpenAI
import yaml
import tiktoken
import platform
import time
import os
import pyperclip

Prints a stylized banner to the console when the application starts.

st.set_page_config(
    page_title="Note-AI",
)

@st.cache_data
def print_banner():
    print("""
        _   __      __             ___    ____
       / | / /___  / /____        /   |  /  _/
      /  |/ / __ \\/ __/ _ \\______/ /| |  / /
     / /|  / /_/ / /_/  __/_____/ ___ |_/ /
    /_/ |_/\\____/\\__/\\___/     /_/  |_/___/
    """)
    return 1

print_banner()
st.logo("https://ea-books.netlify.app/lit/ai_note.svg")

OpenAI client.

client = OpenAI()

Reads prompts from a YAML configuration file (openai_helper.yml). Each prompt entry includes a unique name, a descriptive note explaining its purpose, and an optional list of tags for categorization.

The expected YAML structure is:

- name: grammar
  note: You will be provided with statements in markdown, and your task is to convert them to standard English.
  tags:
    - text

- name: improve_style
  note: Improve style of the content you are provided.
  tags:
    - text

- name: summarize_md
  note: You will be provided with statements in markdown, and your task is to summarize the content.
  tags:
    - text

- name: explain_python
  note: Explain Python code you are provided.
  tags:
    - python

- name: write_python
  note: Write Python code to satisfy the description you are provided.
  tags:
    - python

Each item in the list represents a single prompt and must define:

  • name: A short, unique identifier for the prompt.

  • note: Prompt body.

  • tags: A list of categories (for example, text or python) that describe the prompt’s domain or usage.

prompts_file = "openai_helper.yml"
with open(prompts_file, 'r') as file:
    prompts = yaml.safe_load(file)

Text area to provide input text

input_text = st.text_area(f"Note", height=300)

See: PersistedList

from PersistedList import PersistedList
tags_persisted = PersistedList(".tags")
prompts_persisted = PersistedList(".prompts")
llms_persisted = PersistedList(".llms")
efforts_persisted = PersistedList(".efforts")
  1. Select category, aka group, aka tag.

all_tags_set = {tag for item in prompts for tag in item.get('tags', [])}
all_tags = tags_persisted.sort_by_pattern(list(all_tags_set))

tag_name = st.sidebar.selectbox(
   "Category",
   all_tags,
)
get_prompt(name)

Select prompt body by its name

def get_prompt(name):
    for entry in prompts:
        if entry['name'] == name:
            return entry.get('note')
    return None
has_tag(name, tag_name)
def has_tag(name, tag_name):
    for entry in prompts:
        if entry['name'] == name:
            return tag_name in entry.get('tags', [])
    return False
  1. Select prompt.

all_prompt_names_set = {item['name'] for item in prompts}

all_prompt_names = prompts_persisted.sort_by_pattern(
    list(all_prompt_names_set),
    group=tag_name
)
prompt_names = [name for name in all_prompt_names if has_tag(name, tag_name)]

prompt_name = st.sidebar.selectbox(
   "Prompt",
   prompt_names,
)
prompt = get_prompt(prompt_name)
st.write(prompt)
  1. Select OpenAI LLM model

llm_prices = {
    "gpt-5.6-sol": (5.00, 30.00),
    "gpt-5.6-terra": (2.50, 15.00),
    "gpt-5.6-luna": (1.00, 6.00),
    "gpt-5.5": (5.00, 30.00),
    "gpt-5.4": (2.50, 15.00),
    "gpt-5.4-mini": (0.75, 4.50),
    "gpt-5.4-nano": (0.20, 1.25),
    "gpt-4o-mini": (0.15, 0.60),
    "gpt-4.1-nano": (0.10, 0.40),
}

llm_models = list(llm_prices.keys())

all_llm_models = llms_persisted.sort_by_pattern(
    llm_models,
    group=f"{tag_name}/{prompt_name}"
)

llm_model = st.sidebar.selectbox(
   "LLM Model",
   all_llm_models
)
  1. Select reasoning effort

reasoning_efforts = [
    "none",
    "low",
    "medium",
    "high",
    "xhigh",
    "max",
]

all_reasoning_efforts = efforts_persisted.sort_by_pattern(
    reasoning_efforts,
    group=f"{tag_name}/{prompt_name}/{llm_model}"
)

reasoning_effort = st.sidebar.selectbox(
   "Reasoning",
   all_reasoning_efforts
)

Count the number of tokens in the user’s input using the tiktoken library, and display both the token count and the corresponding price.

encoding = tiktoken.get_encoding("o200k_base")
tokens = encoding.encode(input_text)

cents = round(len(tokens) * llm_prices[llm_model][0]/10000, 5)

st.sidebar.write(f'''
    | Chars | Tokens | Cents |
    |---|---|---|
    | {len(input_text)} | {len(tokens)} | {cents} |
    ''')
call_llm(text, prompt)
def call_llm(text, prompt):
    response = client.responses.create(
        model=llm_model,
        reasoning={"effort": reasoning_effort},
        instructions=prompt,
        input=input_text
    )

    return response.output_text

Run Query

if st.button('Query', type="primary", icon=":material/cyclone:", width="stretch"):
    start_time = time.time()

    # Call LLM
    st.session_state.llm_output = call_llm(input_text, prompt)
    # st.write(st.session_state.llm_output)

    # Calculate and print execution time
    end_time = time.time()
    execution_time = end_time - start_time
    st.session_state.execution_time = end_time - start_time

    # Calculate output price
    tokens = encoding.encode(st.session_state.llm_output)
    st.session_state.output_price = len(tokens) * llm_prices[llm_model][1]/10000

    # Remember persisted selections
    tags_persisted.select(tag_name)
    prompts_persisted.select(prompt_name, group=tag_name)
    llms_persisted.select(llm_model, group=f"{tag_name}/{prompt_name}")
    efforts_persisted.select(reasoning_effort, group=f"{tag_name}/{prompt_name}/{llm_model}")

    if platform.system() == 'Darwin':
        os.system("afplay /System/Library/Sounds/Glass.aiff")
    st.rerun()

LLM output is cached in session_state.

if "llm_output" not in st.session_state:
    st.stop()

st.write('---')
st.write(st.session_state.llm_output)

if st.button("Clipboard", icon=":material/content_copy:"):
    pyperclip.copy(st.session_state.llm_output)
    st.write(f'Copied to clipboard')

Show last execution time

if "execution_time" in st.session_state:
    st.sidebar.write(f"Execution time: `{round(st.session_state.execution_time, 2)}` sec")

if "output_price" in st.session_state:
    st.sidebar.write(f"Output price: `{round(st.session_state.output_price, 5)}` cents")

Environment Setup

Option 1. With Miniconda

To set up your environment using 🐍  Miniconda, follow the steps below. These instructions will guide you through installing Miniconda, configuring your environment, and running a Streamlit application tailored for AI tasks.

Step 1: Install Miniconda

First, you need to install Miniconda. Visit 🛠  Miniconda installation page and follow the instructions for your operating system.

Step 2: Configure Your Environment

  1. Create the Environment File

    Create a file named environment.yml in your project directory. Paste the following contents into this file:

    name: ai-0.1
    channels:
      - conda-forge
      - defaults
    dependencies:
      - python=3.12.0
      - openai
      - tiktoken
      - streamlit
      - pyperclip
    
  2. Select conda-forge Channel

    Open your terminal or command prompt and execute the following commands to prioritize the conda-forge channel:

    conda config --add channels conda-forge
    conda config --set channel_priority strict
    
  3. Create the Environment

    Still in your terminal, navigate to the directory containing your environment.yml file. Create the Conda environment by running:

    conda env create -f environment.yml
    

Step 3: Activate the Environment

Activate your newly created environment by executing:

conda activate ai-0.1

Step 4: Prepare Prompt File

Create a file named openai_helper.yml in your project directory. This file should contain various prompts for the tasks you want to accomplish. You can include tags in your prompts to categorize them.



Step 5: Run Streamlit Script

With your environment set up and activated, and your openai_helper.yml file ready, you’re now set to run your Streamlit application. Execute the following command in your terminal:

streamlit run note_openai.py

And that’s it! Your Streamlit application should now be running, and you can interact with it through your web browser.


Option 2. With venv

To set up your environment using Python’s built-in venv module, follow the steps below. These instructions will guide you through installing Python, creating a virtual environment, installing the required packages, and running a Streamlit application tailored for AI tasks.

Step 1: Install Python

First, make sure Python is installed on your computer. Version 3.12 was used, but other recent Python 3 versions should work as well. Visit the 🐍  Python download page and download an appropriate Python release for your operating system.

On Windows, select the option to add Python to your PATH during installation.

After installation, open a terminal or command prompt and verify the Python version:

python --version

Step 2: Configure Your Environment

  1. Create the Requirements File

    Create a file named requirements.txt in your project directory. Paste the following contents into this file:

    openai
    tiktoken
    streamlit
    pyperclip
    
  2. Create the Virtual Environment

    Open a terminal or command prompt and navigate to your project directory:

    cd path/to/your/project
    

    Create a virtual environment named .venv by running:

    python -m venv .venv
    

    The .venv directory contains an isolated Python environment for this project. It should not normally be committed to version control.

Step 3: Activate the Environment

Activate the newly created virtual environment using the command for your operating system and terminal.

Windows Command Prompt:

.venv\Scripts\activate.bat

Windows PowerShell:

.\.venv\Scripts\Activate.ps1

macOS or Linux:

source .venv/bin/activate

After activation, your terminal prompt should display (.venv).

Step 4: Install the Dependencies

With the virtual environment activated, update pip:

python -m pip install --upgrade pip

Install the packages listed in requirements.txt:

python -m pip install -r requirements.txt

You can verify that the required packages were installed by running:

python -m pip list

Step 5: Prepare Prompt File

Create a file named openai_helper.yml in your project directory. This file should contain the prompts for the tasks you want to accomplish.

You can include tags in your prompts to organize them into categories.



Step 6: Run Streamlit Script

With the virtual environment activated and the openai_helper.yml file ready, run the Streamlit application by executing:

streamlit run note_openai.py

The Streamlit application should start locally and open in your default web browser.

Step 7: Deactivate the Environment

When you are finished working with the application, deactivate the virtual environment by running:

deactivate

To use the application again later, navigate to the project directory, activate .venv, and run the Streamlit command again.