• Stars
    star
    3,463
  • Rank 12,869 (Top 0.3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 1 year ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python package for easily interfacing with chat apps, with robust features and minimal code complexity.

simpleaichat

from simpleaichat import AIChat

ai = AIChat(system="Write a fancy GitHub README based on the user-provided project name.")
ai("simpleaichat")

simpleaichat is a Python package for easily interfacing with chat apps like ChatGPT and GPT-4 with robust features and minimal code complexity. This tool has many features optimized for working with ChatGPT as fast and as cheap as possible, but still much more capable of modern AI tricks than most implementations:

  • Create and run chats with only a few lines of code!
  • Optimized workflows which minimize the amount of tokens used, reducing costs and latency.
  • Run multiple independent chats at once.
  • Minimal codebase: no code dives to figure out what's going on under the hood needed!
  • Chat streaming responses and the ability to use tools.
  • Async support, including for streaming and tools.
  • Ability to create more complex yet clear workflows if needed, such as Agents. (Demo soon!)
  • Coming soon: more chat model support (PaLM, Claude)!

Here's some fun, hackable examples on how simpleaichat works:

Installation

simpleaichat can be installed from PyPI:

pip3 install simpleaichat

Quick, Fun Demo

You can demo chat-apps very quickly with simpleaichat! First, you will need to get an OpenAI API key, and then with one line of code:

from simpleaichat import AIChat

AIChat(api_key="sk-...")

And with that, you'll be thrust directly into an interactive chat!

This AI chat will mimic the behavior of OpenAI's webapp, but on your local computer!

You can also pass the API key by storing it in an .env file with a OPENAI_API_KEY field in the working directory (recommended), or by setting the environment variable of OPENAI_API_KEY directly to the API key.

But what about creating your own custom conversations? That's where things get fun. Just input whatever person, place or thing, fictional or nonfictional, that you want to chat with!

AIChat("GLaDOS")  # assuming API key loaded via methods above

But that's not all! You can customize exactly how they behave too with additional commands!

AIChat("GLaDOS", "Speak in the style of a Seinfeld monologue")

AIChat("Ronald McDonald", "Speak using only emoji")

Need some socialization immediately? Once simpleaichat is installed, you can also start these chats directly from the command line!

simpleaichat
simpleaichat "GlaDOS"
simpleaichat "GLaDOS" "Speak in the style of a Seinfeld monologue"

Building AI-based Apps

The trick with working with new chat-based apps that wasn't readily available with earlier iterations of GPT-3 is the addition of the system prompt: a different class of prompt that guides the AI behavior throughout the entire conversation. In fact, the chat demos above are actually using system prompt tricks behind the scenes! OpenAI has also released an official guide for system prompt best practices to building AI apps.

For developers, you can instantiate a programmatic instance of AIChat by explicitly specifying a system prompt, or by disabling the console.

ai = AIChat(system="You are a helpful assistant.")
ai = AIChat(console=False)  # same as above

You can also pass in a model parameter, such as model="gpt-4" if you have access to GPT-4, or model="gpt-3.5-turbo-16k" for a larger-context-window ChatGPT.

You can then feed the new ai class with user input, and it will return and save the response from ChatGPT:

response = ai("What is the capital of California?")
print(response)
The capital of California is Sacramento.

Alternatively, you can stream responses by token with a generator if the text generation itself is too slow:

for chunk in ai.stream("What is the capital of California?", params={"max_tokens": 5}):
    response_td = chunk["response"]  # dict contains "delta" for the new token and "response"
    print(response_td)
The
The capital
The capital of
The capital of California
The capital of California is

Further calls to the ai object will continue the chat, automatically incorporating previous information from the conversation.

response = ai("When was it founded?")
print(response)
Sacramento was founded on February 27, 1850.

You can also save chat sessions (as CSV or JSON) and load them later. The API key is not saved so you will have to provide that when loading.

ai.save_session()  # CSV, will only save messages
ai.save_session(format="json", minify=True)  # JSON

ai.load_session("my.csv")
ai.load_session("my.json")

Functions

A large number of popular venture-capital-funded ChatGPT apps don't actually use the "chat" part of the model. Instead, they just use the system prompt/first user prompt as a form of natural language programming. You can emulate this behavior by passing a new system prompt when generating text, and not saving the resulting messages.

The AIChat class is a manager of chat sessions, which means you can have multiple independent chats or functions happening! The examples above use a default session, but you can create new ones by specifying a id when calling ai.

json = '{"title": "An array of integers.", "array": [-1, 0, 1]}'
functions = [
             "Format the user-provided JSON as YAML.",
             "Write a limerick based on the user-provided JSON.",
             "Translate the user-provided JSON from English to French."
            ]
params = {"temperature": 0.0, "max_tokens": 100}  # a temperature of 0.0 is deterministic

# We namespace the function by `id` so it doesn't affect other chats.
# Settings set during session creation will apply to all generations from the session,
# but you can change them per-generation, as is the case with the `system` prompt here.
ai = AIChat(id="function", params=params, save_messages=False)
for function in functions:
    output = ai(json, id="function", system=function)
    print(output)
title: "An array of integers."
array:
  - -1
  - 0
  - 1
An array of integers so neat,
With values that can't be beat,
From negative to positive one,
It's a range that's quite fun,
This JSON is really quite sweet!
{"titre": "Un tableau d'entiers.", "tableau": [-1, 0, 1]}

Newer versions of ChatGPT also support "function calling", but the real benefit of that feature is the ability for ChatGPT to support structured input and/or output, which now opens up a wide variety of applications! simpleaichat streamlines the workflow to allow you to just pass an input_schema and/or an output_schema.

You can construct a schema using a pydantic BaseModel.

from pydantic import BaseModel, Field

ai = AIChat(
    console=False,
    save_messages=False,  # with schema I/O, messages are never saved
    model="gpt-3.5-turbo-0613",
    params={"temperature": 0.0},
)

class get_event_metadata(BaseModel):
    """Event information"""

    description: str = Field(description="Description of event")
    city: str = Field(description="City where event occured")
    year: int = Field(description="Year when event occured")
    month: str = Field(description="Month when event occured")

# returns a dict, with keys ordered as in the schema
ai("First iPhone announcement", output_schema=get_event_metadata)
{'description': 'The first iPhone was announced by Apple Inc.',
 'city': 'San Francisco',
 'year': 2007,
 'month': 'January'}

See the TTRPG Generator Notebook for a more elaborate demonstration of schema capabilities.

Tools

One of the most recent aspects of interacting with ChatGPT is the ability for the model to use "tools." As popularized by LangChain, tools allow the model to decide when to use custom functions, which can extend beyond just the chat AI itself, for example retrieving recent information from the internet not present in the chat AI's training data. This workflow is analogous to ChatGPT Plugins.

Parsing the model output to invoke tools typically requires a number of shennanigans, but simpleaichat uses a neat trick to make it fast and reliable! Additionally, the specified tools return a context for ChatGPT to draw from for its final response, and tools you specify can return a dictionary which you can also populate with arbitrary metadata for debugging and postprocessing. Each generation returns a dictionary with the response and the tool function used, which can be used to set up workflows akin to LangChain-style Agents, e.g. recursively feed input to the model until it determines it does not need to use any more tools.

You will need to specify functions with docstrings which provide hints for the AI to select them:

from simpleaichat.utils import wikipedia_search, wikipedia_search_lookup

# This uses the Wikipedia Search API.
# Results from it are nondeterministic, your mileage will vary.
def search(query):
    """Search the internet."""
    wiki_matches = wikipedia_search(query, n=3)
    return {"context": ", ".join(wiki_matches), "titles": wiki_matches}

def lookup(query):
    """Lookup more information about a topic."""
    page = wikipedia_search_lookup(query, sentences=3)
    return page

params = {"temperature": 0.0, "max_tokens": 100}
ai = AIChat(params=params, console=False)

ai("San Francisco tourist attractions", tools=[search, lookup])
{'context': "Fisherman's Wharf, San Francisco, Tourist attractions in the United States, Lombard Street (San Francisco)",
 'titles': ["Fisherman's Wharf, San Francisco",
  'Tourist attractions in the United States',
  'Lombard Street (San Francisco)'],
 'tool': 'search',
 'response': "There are many popular tourist attractions in San Francisco, including Fisherman's Wharf and Lombard Street. Fisherman's Wharf is a bustling waterfront area known for its seafood restaurants, souvenir shops, and sea lion sightings. Lombard Street, on the other hand, is a famous winding street with eight hairpin turns that attract visitors from all over the world. Both of these attractions are must-sees for anyone visiting San Francisco."}
ai("Lombard Street?", tools=[search, lookup])
{'context': 'Lombard Street is an east–west street in San Francisco, California that is famous for a steep, one-block section with eight hairpin turns. Stretching from The Presidio east to The Embarcadero (with a gap on Telegraph Hill), most of the street\'s western segment is a major thoroughfare designated as part of U.S. Route 101. The famous one-block section, claimed to be "the crookedest street in the world", is located along the eastern segment in the Russian Hill neighborhood.',
 'tool': 'lookup',
 'response': 'Lombard Street is a famous street in San Francisco, California known for its steep, one-block section with eight hairpin turns. It stretches from The Presidio to The Embarcadero, with a gap on Telegraph Hill. The western segment of the street is a major thoroughfare designated as part of U.S. Route 101, while the famous one-block section, claimed to be "the crookedest street in the world", is located along the eastern segment in the Russian Hill'}
ai("Thanks for your help!", tools=[search, lookup])
{'response': "You're welcome! If you have any more questions or need further assistance, feel free to ask.",
 'tool': None}

Miscellaneous Notes

  • Like gpt-2-simple before it, the primary motivation behind releasing simpleaichat is to both democratize access to ChatGPT even more and also offer more transparency for non-engineers into how Chat AI-based apps work under the hood given the disproportionate amount of media misinformation about their capabilities. This is inspired by real-world experience from my work with BuzzFeed in the domain, where after spending a long time working with the popular LangChain, a more-simple implementation was both much easier to maintain and resulted in much better generations. I began focusing development on simpleaichat after reading a Hacker News thread filled with many similar complaints, indicating value for an easier-to-use interface for modern AI tricks.
    • simpleaichat very intentionally avoids coupling features with common use cases where possible (e.g. Tools) in order to avoid software lock-in due to the difficulty implementing anything not explicitly mentioned in the project's documentation. The philosophy behind simpleaichat is to provide good demos, and let the user's creativity and business needs take priority instead of having to fit a round peg into a square hole like with LangChain.
    • simpleaichat makes it easier to interface with Chat AIs, but it does not attempt to solve common technical and ethical problems inherent to large language models trained on the internet, including prompt injection and unintended plagiarism. The user should exercise good judgment when implementing simpleaichat. Use cases of simpleaichat which go against OpenAI's usage policies (including jailbreaking) will not be endorsed.
    • simpleaichat intentionally does not use the "Agent" logical metaphor for tool workflows because it's become an AI hype buzzword heavily divorced from its origins. If needed be, you can emulate the Agent workflow with a while loop without much additional code, plus with the additional benefit of much more flexibility such as debugging.
  • The session manager implements some sensible security defaults, such as using UUIDs as session ids by default, storing authentication information in a way to minimize unintentional leakage, and type enforcement via Pydantic. Your end-user application should still be aware of potential security issues, however.
  • Although OpenAI's documentation says that system prompts are less effective than a user prompt constructed in a similar manner, in my experience it still does perform better for maintaining rules/a persona.
  • Many examples of popular prompts use more conversational prompts, while the example prompts here use more consise and imperative prompts. This aspect of prompt engineering is still evolving, but in my experience commands do better with ChatGPT and with greater token efficieny. That's also why simpleaichat allows users to specify system prompts (and explicitly highlights what the default use) instead of relying on historical best practices.
  • Token counts for async is not supported as OpenAI doesn't return token counts when streaming responses. In general, there may be some desync in token counts and usage for various use cases; I'm working on categorizing them.
  • Outside of the explicit examples, none of this README uses AI-generated text. The introduction code example is just a joke, but it was too good of a real-world use case!

Roadmap

  • PaLM Chat (Bard) and Anthropic Claude support
  • More fun/feature-filled CLI chat app based on Textual
  • Simple example of using simpleaichat in a webapp
  • Simple of example of using simpleaichat in a stateless manner (e.g. AWS Lambda functions)

Maintainer/Creator

Max Woolf (@minimaxir)

Max's open-source projects are supported by his Patreon and GitHub Sponsors. If you found this project helpful, any monetary contributions to the Patreon are appreciated and will be put to good creative use.

License

MIT

More Repositories

1

big-list-of-naughty-strings

The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data.
Python
46,104
star
2

textgenrnn

Easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code.
Python
4,941
star
3

hacker-news-undocumented

Some of the hidden norms about Hacker News not otherwise covered in the Guidelines and the FAQ.
3,616
star
4

gpt-2-simple

Python package to easily retrain OpenAI's GPT-2 text-generating model on new texts
Python
3,402
star
5

facebook-page-post-scraper

Data scraper for Facebook Pages, and also code accompanying the blog post How to Scrape Data From Facebook Page Posts for Statistical Analysis
Python
2,116
star
6

person-blocker

Automatically "block" people in images (like Black Mirror) using a pretrained neural network.
Python
2,022
star
7

automl-gs

Provide an input CSV and a target field to predict, generate a model + code to run it.
Python
1,845
star
8

aitextgen

A robust Python tool for text-based AI training and generation using GPT-2.
Python
1,831
star
9

stylecloud

Python package + CLI to generate stylistic wordclouds, including gradients and icon shapes!
Python
825
star
10

gpt-3-experiments

Test prompts for OpenAI's GPT-3 API and the resulting AI-generated texts.
Python
702
star
11

video-to-gif-osx

A set of utilities that allow the user to easily convert video files to very-high-quality GIFs on OS X.
Shell
395
star
12

copy-syntax-highlight-osx

Copy Syntax Highlight for OS X is an OS X service which copies the selected text to the clipboard, with proper syntax highlighting for the given language.
381
star
13

gpt-2-cloud-run

Text-generation API via GPT-2 for Cloud Run
HTML
313
star
14

reactionrnn

Python module + R package to predict the reactions to a given text using a pretrained recurrent neural network.
Python
299
star
15

gpt-2-keyword-generation

Method to encode text for GPT-2 to generate text based on provided keywords
Python
260
star
16

download-tweets-ai-text-gen

Python script to download public Tweets from a given Twitter account into a format suitable for AI text generation.
Python
220
star
17

tweet-generator

Train a neural network optimized for generating tweets based off of any number of Twitter users.
Python
218
star
18

char-embeddings

A repository containing 300D character embeddings derived from the GloVe 840B/300D dataset, and uses these embeddings to train a deep learning model to generate Magic: The Gathering cards using Keras
Python
214
star
19

magic-the-gifening

A Twitter bot which tweets Magic: the Gathering cards with appropriate GIFs superimposed onto them.
Python
212
star
20

system-dashboard

Minimalist Win/OSX/Linux System Dashboard using Flask and Freeboard
HTML
200
star
21

imgmaker

Create high-quality images programmatically with easily-hackable templates.
Python
175
star
22

ctrl-gce

Set up the CTRL text-generating model on Google Compute Engine with just a few console commands.
Shell
151
star
23

ai-generated-pokemon-rudalle

Python script to preprocess images of all Pokémon to finetune ruDALL-E
Python
138
star
24

imgbeddings

Python package to generate image embeddings with CLIP without PyTorch/TensorFlow
Python
134
star
25

mtg-gpt-2-cloud-run

Code and UI for running a Magic card text generator API via GPT-2
HTML
120
star
26

get-all-hacker-news-submissions-comments

Simple Python scripts to download all Hacker News submissions and comments and store them in a PostgreSQL database.
Python
119
star
27

hacker-news-gpt-2

Dump of generated texts from GPT-2 trained on Hacker News titles
117
star
28

facebook-ad-library-scraper

A Python scraper for the Facebook Ad Library, using the official Facebook Ad Library API.
Python
114
star
29

reddit-bigquery

Code + Jupyter notebook for analyzing and visualizing Reddit Data quickly and easily
R
112
star
30

optillusion-animation

Python code to submit rotated images to the Cloud Vision API + R code for visualizing it
Python
99
star
31

chatgpt_api_test

Demos utilizing the ChatGPT API
Jupyter Notebook
96
star
32

gpt-3-client

A client for OpenAI's GPT-3 API for ad hoc testing of prompt without using the web interface.
Python
90
star
33

stable-diffusion-negative-prompt

Jupyter Notebooks for experimenting with negative prompting with Stable Diffusion 2.0.
Jupyter Notebook
87
star
34

stylistic-word-clouds

Python scripts for creating stylistic word clouds
Python
85
star
35

gpt3-blog-title-optimizer

Python code for building a GPT-3 based technical blog post optimizer.
Jupyter Notebook
83
star
36

amazon-spark

R Code + R Notebook for analyzing millions of Amazon reviews using Apache Spark
HTML
83
star
37

twcloud

Python package + CLI to generate wordclouds of Twitter tweets.
Python
76
star
38

twitter-cloud-run

A (relatively) minimal configuration app to run Twitter bots on a schedule that can scale to unlimited bots.
Python
76
star
39

deep-learning-cpu-gpu-benchmark

Repository to benchmark the performance of Cloud CPUs vs. Cloud GPUs on TensorFlow and Google Compute Engine.
HTML
67
star
40

get-profile-data-of-repo-stargazers

This repository contains a script used to get the GitHub profile information of all the people who've Stared a given GitHub repository
Python
67
star
41

icon-image

Python script to quickly generate a Font Awesome icon imposed on a background for steering AI image generation.
Python
53
star
42

gpt-j-6b-experiments

Test prompts for GPT-J-6B and the resulting AI-generated texts
53
star
43

ml-data-generator

Python script to generate fake datasets optimized for testing machine learning/deep learning workflows
Python
51
star
44

hacker-news-download-all-stories

Download *ALL* the submissions from Hacker News
Python
51
star
45

clickbait-cluster

Code + Jupyter Notebooks for Visualizing Clusters of Clickbait Headlines Using Spark, Word2vec, and Plotly
HTML
47
star
46

keras-cntk-docker

Docker container for keras + cntk intended for nvidia-docker
Python
42
star
47

foursquare-venue-scraper

A Foursquare data scraper that gathers all venues within a specified geographic area.
Python
39
star
48

interactive-facebook-reactions

Jupyter notebook + Code for processing Facebook Reactions data and making Interactive Charts
HTML
38
star
49

youtube-video-scraper

Tools for scraping YouTube video metadata (mostly for training AI on video titles)
Python
38
star
50

nyc-taxi-notebook

R Code + Jupyter notebook for analyzing and visualizing NYC Taxi data
R
31
star
51

sdxl-experiments

Jupyter Notebooks for experimenting with Stable Diffusion XL 1.0
Jupyter Notebook
30
star
52

yelp-review-analysis

Repository containing script on how I processed and charted Yelp data.
R
29
star
53

langchain-problems

Demos of some issues with LangChain.
Jupyter Notebook
29
star
54

subreddit-generator

Train a neural network optimized for generating Reddit subreddit posts
Python
28
star
55

predict-reddit-submission-success

Repository w/ Jupyter + R Notebooks for creating a model to predict the success of Reddit submissions with Keras.
HTML
28
star
56

autotweet-from-googlesheet

A minimal proof-of-concept Python script to tweet human-curated Tweets on a schedule.
Python
27
star
57

tritonize

Convert images to a styled, minimal representation, quickly with NumPy
Python
27
star
58

keras-cntk-benchmark

Code for Benchmarking CNTK performance on Keras vs. TensorFlow
Python
26
star
59

frames-to-gif-osx

An application that allows the user to easily convert frames to very-high-quality GIFs on OS X.
26
star
60

minimaxir.github.io

Blog Posts and Theme for https://minimaxir.com
HTML
25
star
61

ggplot-tutorial

Repository for ggplot2 tutorial
R
24
star
62

legaladvice-gpt2

Dump of generated texts from GPT-2 trained on /r/legaladvice subreddit titles
23
star
63

chatgpt-structured-data

Demos of ChatGPT's function calling/structured data support.
Jupyter Notebook
22
star
64

sf-arrests-when-where

R Code + Jupyter notebook for replicating analysis of when and where arrests in San Francisco occur.
R
22
star
65

pokemon-3d

Code + Visualizations processing and visualizing Pokémon data in 3D
HTML
21
star
66

reddit-gpt-2-cloud-run

Reddit title generator API based on GPT-2
HTML
20
star
67

facebook-keyword-regression-analysis

Regression Analysis for Facebook keywords.
R
20
star
68

chatgpt-tips-analysis

Jupyter Notebooks for testing the impact of tip incentives for ChatGPT
Jupyter Notebook
20
star
69

stylecloud-examples

Examples of stylistic word clouds generated via the stylecloud Python package
Python
19
star
70

stack-overflow-survey

Code + Visualizations for processing 2016 Stack Overflow Survey Data
Jupyter Notebook
19
star
71

get-heart-rate-csv

A small Python script to get the heart rate data generated from an Apple Watch in a CSV form
Python
19
star
72

get-bars-from-foursquare

A quick pair of Python scripts to retrieve all bars within a given area, then retrieve metadata and process it.
Python
19
star
73

subreddit-related

Code and visualizations for related/similar subreddits
Jupyter Notebook
19
star
74

ai-generated-magic-cards

Tools for encoding Magic: The Gathering cards into a form suitable for AI text generation
Python
17
star
75

tensorflow-multiprocess-ray

Proof of concept on how to use TensorFlow for prediction tasks in a multiprocess setting.
Python
17
star
76

pokemon-ai

A text-generating AI to generate Pokémon names.
Python
17
star
77

reddit-comment-length

R code needed to reproduce Relationship between Reddit Comment Score and Comment Length for 1.66 Billion Comments visualization
R
17
star
78

mtg-card-creator-api

Code for running a Magic card image generator API
Python
16
star
79

automl-gs-examples

Examples + Visualizations of datasets modeled using automl-gs
Python
16
star
80

reddit-graph

Jupyter notebook + Code for reproducing Reddit Subreddit graphs
Jupyter Notebook
16
star
81

ncaa-basketball

R Code + R Notebook on how to process and visualize NCAA basketball data.
R
16
star
82

pokemon-embeddings

Jupyter Notebooks and an R Notebook for encoding Pokémon embeddings and creating data visualizations.
Jupyter Notebook
16
star
83

sfba-compensation

Jupyter notebook + Code for scraping AngelList data and making an interactive chart of SFBA salaries/equity
HTML
14
star
84

resetera-gpt-2

Scraper of ResetEra threads and posts to get them into a format suitable for feeding them into GPT-2.
Python
14
star
85

get-data-from-photos-from-instagram-tags

Processes data from images which are tagged with the specified Instagram tag.
Python
13
star
86

hacker-news-comment-analysis

Code used for analysis of Hacker News comments.
R
13
star
87

char-tsne-visualization

Visualizations of character embeddings from derived character vectors.
HTML
13
star
88

imdb-data-analysis

R Code + R Notebook on how to process and visualize the official IMDb datasets.
12
star
89

hn-heatmaps

Code and data necessary to reproduce heatmaps relating HN Submission time to submission score.
R
12
star
90

sf-crimes-covid

Spot checking impact of SF shelter-in-places on crime reporting.
12
star
91

imgur-decline

R Code + R Notebook for analyzing the decline of Imgur on Reddit.
HTML
11
star
92

gpt-2-fanfiction

Experiments with generating GPT-2 fanfiction on specified topics.
11
star
93

notebooks

This GitHub Repository stores my R Notebooks, allowing GitHub Pages to serve the R Notebooks on my website
HTML
11
star
94

all-marvel-comics-characters

Creates a .csv of all Marvel Comics Characters + Statistics via the Marvel API
Python
10
star
95

movie-gender

Data and code for analyzing Movie Lead Gender.
Jupyter Notebook
10
star
96

online-class-charts

Code needed to reproduce data analysis and charts for MIT/Harvard Online Course Data
R
9
star
97

ggplot2-web

R Code + R Notebook on how to make high quality data visualizations on the web with ggplot2.
HTML
9
star
98

reddit-subreddit-keywords

Code + Jupyter notebook for analyzing and visualizing means and medians of keywords in the top Reddit Subreddits.
R
8
star
99

reddit-mean-score

Quick data visualization for Reddit Mean Submission Score by Subreddit
8
star
100

sf-arrests-predict

R Code + R Notebook for predicting arrest types in San Francisco.
HTML
8
star