Skip to content

Commit

Permalink
Tutor-GPT 0.5.0 (#124)
Browse files Browse the repository at this point in the history
* Tracing (#60)

* Tracing to separate conversations

* Basic Postgres Connection via Supabase

* MVP Postgres Backed Chat Message History

* Full binary install or psycopg3

* Use Supabase directly to avoid issues with postgres connections

* .env.template update

* 0.3.0 Long Term Memory (#62)

* Add reply to discord and limit chat history to 10 messages

* reverse returned messages and cleanup comments

* Update changelog

* increase cache limit

* Switch to Azure OpenAI

* Changelog

* fix: update link to more recent blog post

* Sanitize thoughts before sending them in the thought's channel (#65)

* LayeredLRU Cache (#69)

* Make BloomChain a static class

* MVP Layered LRU Cache using Postgres

* Remove testing variables

* Async Mutex Lock on Cache

* Stateless bug (#70)

* Dynamic model switching

* Fix hardcoding and add recovery logic for conversation tables

* Add support for multiple conversations

* Fix merge errors

* Fix merge errors 2

* fix: top_p 0.5 to address gibberish

* Custom Web UI (#76)

* init nextjs

* fast api init

* styling and thoughts

* streaming updates

* connect to api

* Add thoughts to the web UI

* Refactor input to be a form for UX (e.g. pressing enter sends)

* typing and thoughts

* Refactor input to be a form for UX (e.g. pressing enter sends)

* Revert "Merge remote-tracking branch 'origin/custom-web' into custom-web"

This reverts commit 1eae747.

* Skeleton Multiple Chat Window UI

* MVP Layout

* Tested Discord and Skeleton FastAPI

* Add, Delete, and Set Conversations

* Get and send messages

* Edit message names

* Local serving from FastAPI via static export

* Deployment strategy for static files

* Separate out apps

* Vercel Deployment with Action

* Re-add discord to fly.toml

---------

Co-authored-by: hyusap <[email protected]>
Co-authored-by: Jacob Van Meter <[email protected]>

* Fix Github Action Workflow

* Fix Github Action Workflow

* add user prediction function

* Honcho Changes (#77)

* init nextjs

* fast api init

* styling and thoughts

* streaming updates

* connect to api

* Add thoughts to the web UI

* Refactor input to be a form for UX (e.g. pressing enter sends)

* typing and thoughts

* Refactor input to be a form for UX (e.g. pressing enter sends)

* Revert "Merge remote-tracking branch 'origin/custom-web' into custom-web"

This reverts commit 1eae747.

* Skeleton Multiple Chat Window UI

* MVP Layout

* Tested Discord and Skeleton FastAPI

* Add, Delete, and Set Conversations

* Get and send messages

* Edit message names

* Local serving from FastAPI via static export

* Deployment strategy for static files

* Separate out apps

* Vercel Deployment with Action

* Re-add discord to fly.toml

* Honcho Stream

* Honcho Stream

---------

Co-authored-by: hyusap <[email protected]>
Co-authored-by: Jacob Van Meter <[email protected]>

* Social Graph Changes

* Authentication Form Styling

* Working Auth

* Stylistic changes

* Address all linting errors

* Naive Route Protection

* Fly minimum machines

* Open Graph Image Changes

* Remove anonymous honcho usage, fix opengraph (#80)

* Remove anonymous honcho usage, fix opengraph

* Remove extra excess logging and comment

* UI tweaks (#81)

* Remove anonymous honcho usage, fix opengraph

* Remove extra excess logging and comment

* I hate typescript

* UI tweaks (#82)

* Remove anonymous honcho usage, fix opengraph

* Remove extra excess logging and comment

* I hate typescript

* Open Graph, Block chat, delete convo

* Sign Up UI

* Sign Up UI 2

* Change open graph image

* Open Graph Fix

* Remove Streamlit

* UI tweaks (#84)

* Remove anonymous honcho usage, fix opengraph

* Remove extra excess logging and comment

* I hate typescript

* Open Graph, Block chat, delete convo

* Sign Up UI

* Sign Up UI 2

* Change open graph image

* Remove native prompts for sweetalert

* Changelog

* Remove Honcho for discord

* Web fixes (#89)

* seperation between sidebar and window + formatting

* make app pwa downloadable

* move to new chat on button press

* remove router

* feat: Refactor code to use MarkdownWrapper component

- Refactored the code in the `Home` component to replace usages of `ReactMarkdown` and `SyntaxHighlighter` with the new `MarkdownWrapper` component.
- Updated the `Home` component to pass the `text` prop to the `MarkdownWrapper` component for each message.
- Added a new component `MarkdownWrapper` to handle rendering markdown text with syntax highlighting.
- Removed unused imports from the `page.tsx` file, including `ReactMarkdown`, `SyntaxHighlighter`, and `dark` styles.
- Also removed unused imports from the `sidebar.tsx` file, including `useState` import.

* Optimization (#96)

* Usable without honcho and move metadata to creation points

* Remove unused psycopg code

* Add Sentry instrumentation

* Sentry to python

* Update env

* Optimization (#98)

* Usable without honcho and move metadata to creation points

* Remove unused psycopg code

* Add Sentry instrumentation

* Sentry to python

* Update env

* Sentry & Posthog integration on web

* Linting Errors

* add latex support and incentive it (#104)

* prevent unallowed messages (#111)

* implement autoscroll (#112)

* implement autoscroll
Refactored imports in "page.tsx" to improve readability and maintainability. Updated the type of the "input" ref to be more specific. Added a new state and ref to track the scroll position of the message container. Set up an event listener to update the state when the scroll position changes. Made adjustments to the scroll position in certain event handlers.

* update scroll

---------

Co-authored-by: Vineeth Voruganti <[email protected]>

* ✨ add multiline support (#118)

* ♻️ refactor all of the api stuff (#119)

* ♻️ refactor all of the api stuff

a redo of the old refactor branch, but cleaner.
moved all api calls to api.ts and supabase.ts

* Minor Bug Fixes

- Add `build-essential` to dockerfile
- Fix signin button size
- Add await for signout
- Force re-render on conversation name change

---------

Co-authored-by: Vineeth Voruganti <[email protected]>

* ✨ implement dark mode (#120)

automatically check for system mode
manual control via animated thingy

Co-authored-by: Vineeth Voruganti <[email protected]>

* Documentation (#121)

* README update and start of supabase plus making next buildable

* Supabase Local Setup and README additions

* Update web .env template and note on supabase local auth

* Force redirect for unauthenticated and add posthog events (#122)

* Update version

* Static banner

---------

Co-authored-by: vintro <[email protected]>
Co-authored-by: Jacob Vanmeter <[email protected]>
Co-authored-by: hyusap <[email protected]>
Co-authored-by: vintro <[email protected]>
  • Loading branch information
5 people authored Dec 8, 2023
1 parent 0288254 commit d4792da
Show file tree
Hide file tree
Showing 22 changed files with 2,415 additions and 1,256 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# https://testdriven.io/blog/docker-best-practices/
FROM python:3.10-slim-bullseye

RUN apt-get update && apt-get install -y build-essential

WORKDIR /app

# https://stackoverflow.com/questions/53835198/integrating-python-poetry-with-docker
Expand Down
183 changes: 159 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,106 @@
# tutor-gpt
![Static Badge](https://img.shields.io/badge/Version-0.4.0-blue)
[![Discord](https://img.shields.io/discord/1076192451997474938?logo=discord&logoColor=%23ffffff&label=Bloom&labelColor=%235865F2)](https://discord.gg/bloombotai)
![GitHub License](https://img.shields.io/github/license/plastic-labs/tutor-gpt)
![GitHub Repo stars](https://img.shields.io/github/stars/plastic-labs/tutor-gpt)
[![X (formerly Twitter) URL](https://img.shields.io/twitter/url?url=https%3A%2F%2Ftwitter.com%2FBloomBotAI&label=Twitter)](https://twitter.com/BloomBotAI)
[![arXiv](https://img.shields.io/badge/arXiv-2310.06983-b31b1b.svg)](https://arxiv.org/abs/2310.06983)

Tutor-GPT is a LangChain LLM application. It dynamically reasons about your learning needs and *updates its own prompts* to best serve you.

We leaned into theory of mind experiments and Bloom is now more than just a literacy tutor, it’s an expansive learning companion. Read more about how it works [here](https://plasticlabs.ai/blog/Theory-of-Mind-is-All-You-Need) or you can join our [Discord](https://discord.gg/bloombotai) to try out our implementation for free (while our OpenAI spend lasts 😄).
Tutor-GPT is a LangChain LLM application developed by [Plastic
Labs](https://plasticlabs.ai). It dynamically reasons about your learning needs
and *updates its own prompts* to best serve you.

Alternatively, you can run your own instance of the bot by following the instructions below.
We leaned into theory of mind experiments and it is now more than just a
literacy tutor, it’s an expansive learning companion. Read more about how it
works [here](https://plasticlabs.ai/blog/Theory-of-Mind-is-All-You-Need).

## Installation
The hosted version of `tutor-gpt` is called [Bloom](https://bloombot.ai) as a
nod to Benjamin Bloom's Two Sigma Problem. You can try the web version at
[chat.bloombot.ai](https://chat.bloombot.ai) or you can join our
[Discord](https://discord.gg/bloombotai) to try out our implementation for free
(while our OpenAI spend lasts 😄).

This project requires docker to be installed and running locally. [Install docker](https://docs.docker.com/get-docker/) and ensure it's running before proceeding.
Alternatively, you can run your own instance of the bot by following the
instructions below.

## Getting Started
## Project Structure

This app requires you to have a few different environment variables set. Create a `.env` file from the `.env.template`.
The tutor-gpt project is split between multiple different modules that split up
the backend logic for different clients.

**OPENAI_API_KEY**: Go to [OpenAI](https://beta.openai.com/account/api-keys) to generate your own API key.
**BOT_TOKEN**: This is the discord bot token. You can find instructions on how to create a bot and generate a token in the [pycord docs](https://guide.pycord.dev/getting-started/creating-your-first-bot).
**THOUGHT_CHANNEL_ID**: This is the discord channel for the bot to output thoughts to. Make a channel in your server and copy the ID by right clicking the channel and copying the link. The channel ID is the last string of numbers in the link.
- `agent/` - this contains the core logic and prompting architecture
- `bot/` - this contains the discord bot implementation
- `api/` - this contains an API interface to the tutor-gpt backend
- `www/` - this contains a `NextJS` web front end that can connect to the API interface
- `common/` - this contains common used in different interfaces
- `supabase/` - contains SQL scripts necessary for setting up local supabase

### Docker/Containerization
Most of the project is developed using python with the exception of the NextJS
application. For python `poetry` is used for dependency management and for the
web interface `yarn` is used.

### Supabase

Additionally, this project uses supabase for managing different users,
authentication, and as the database for holding message and conversation
information. We recommend for testing and local development to use a local instance of supabase. The supabase-cli is the best way to do this.

The repository containers a `Dockerfile` for running the bot in a containerized workflow. Use the following command to build and run the container locally:
Follow the [Supabase Documentation](https://supabase.com/docs/guides/cli/local-development) for more information. The project contains a `supabase/` folder that contains the scaffolding SQL migrations necessary for setting up the necessary tables. Once you have the supabase cli installed you can simply run the below command in the `tutor-gpt` folder and a local instance of Supabase will start up.

> NOTE: Local Supabase relies on docker so ensure docker is also running before running the below command
```bash
docker build -t tutor-gpt:latest .
docker run --env-file .env tutor-gpt
supabase start
```

The current behaviour will utilize the `.env` file in your local repository and
run the bot. There are two separate entry points for tutor-gpt both a discord UI
and a web ui. Below contains snippets for manually specifying the execution
environment.
Another, useful note about doing testing locally with supabase is that there is
no need to verify an account when it is created so you can create a new account
on the webui and then immediately sign in with it.

## Installation

> NOTE: The project uses
> [poetry](https://python-poetry.org/docs/#installing-with-the-official-installer)
> and [yarn](https://yarnpkg.com/getting-started/install) for package
> management.
The below commands will install all the dependencies necessary for running the
tutor-gpt project. We recommend using poetry to setup a virtual environment for
the project.

```bash
docker run --env-file .env tutor-gpt python -u -m bot.app # Discord UI
docker run -p 8501:8501 --env-file .env tutor-gpt python -u -m streamlit run www/main.py # Web UI
git clone https://github.com/plastic-labs/tutor-gpt.git
cd tutor-gpt
poetry install # install Python dependencies
cd www/
yarn install # install all NodeJS dependencies
```

### Architecture
### Docker

Below is high level diagram of the architecture for the bot.
![Tutor-GPT Discord Architecture](assets/ToM&#32;Chain&#32;Flow.png)
Alternatively (The recommended way) this project can be built and run with
docker. [Install docker](https://docs.docker.com/get-docker/) and ensure it's
running before proceeding.

## Contributing
The web front end is built and run separately from the remainder of the
codebase. Below are the commands for building the core of the tutor-gpt project
which includes the necessary dependencies for running either the discord bot or
the FastAPI endpoint.

```bash
git clone https://github.com/plastic-labs/tutor-gpt.git
cd tutor-gpt
docker build -t tutor-gpt-core .
```

Similarly, to build the web interface run the below commands
```bash
cd tutor-gpt/www
docker build -t tutor-gpt-web .
```

> NOTE: for poetry usage
This project uses [poetry](https://python-poetry.org/) to manage dependencies.
To install dependencies locally run `poetry install`. Or alternatively run
Expand All @@ -68,3 +126,80 @@ continue directly with `poetry shell` or wrap the source command like below
```bash
poetry run source $(poetry env info --path)/bin/activate
```

## Usage

This app requires you to have a few different environment variables set. Create
a `.env` file from the `.env.template`. Depending on which interface you are
running (web or discord) different variables are necessary. This is explained
below

### Required
- **OPENAI_API_KEY**: Go to [OpenAI](https://beta.openai.com/account/api-keys) to generate your own API key.
- **SUPABASE_URL**: The base URL for your supabase instance
- **SUPABASE_KEY**: The API key for interacting with your supabase project. This corresponds to the service key, get it from your project settings
- **CONVERSATION_TABLE**: the name of the table to hold conversation metadata
- **MEMORY_TABLE**: the name of the table holding messages for different conversations

### Discord Only
- **BOT_TOKEN**: This is the discord bot token. You can find instructions on how
to create a bot and generate a token in the [pycord
docs](https://guide.pycord.dev/getting-started/creating-your-first-bot).
- **THOUGHT_CHANNEL_ID**: This is the discord channel for the bot to output
thoughts to. Make a channel in your server and copy the ID by right clicking the
channel and copying the link. The channel ID is the last string of numbers in
the link.

### Web Only
- **URL**: the URL that the web ui is running from by default this should be http://localhost:3000

### Web UI Environment

The `NextJS` application in `www/` also has it's own environment variables which are usually held in the .env.local file. There is another `.env.template` file that you can use for getting started. These are explaing below.

- **NEXT_PUBLIC_URL**: The url the web application will be accessible the default with `NextJS` is http://localhost:3000
- **NEXT_PUBLIC_API_URL**: The url the api backend will be run from the default for `FastAPI is` http://localhost:8000
- **NEXT_PUBLIC_SUPABASE_URL**: The url for your supabase project should be identical to the one used in the python backend
- **NEXT_PUBLIC_SUPABASE_ANON_KEY**: The API key for supabase this time it is the anon key NOT the service key
- **NEXT_PUBLIC_SENTRY_DSN**: Optional for sentry bug tracking
- **NEXT_PUBLIC_SENTRY_ENVIRONMENT**: Optional for sentry bug tracking
- **NEXT_PUBLIC_POSTHOG_KEY**: Optional Posthog event tracking
- **NEXT_PUBLIC_POSTHOG_HOST**: Option for Posthog event tracking

---

### Docker/Containerization

You can also optionally use the docker containers to run the application locally. Below is the command to run the discord bot locally using a `.env` file that is not within the docker container. Be careful not to add your `.env` in the docker container as this is insecure and can leak your secrets.

```bash
docker run --env-file .env tutor-gpt-core python -u -m bot.app
```

To run the webui you need to run the backend `FastAPI` and the frontend `NexTJS` containers separately. In two separate terminal instances run the following commands to have both applications run.
The current behaviour will utilize the `.env` file in your local repository and
run the bot.

```bash
docker run -p 8000:8000 --env-file .env tutor-gpt-core python -m uvicorn api.main:app --host 0.0.0.0 --port 8000 # FastAPI Backend
docker run tutor-gpt-web
```

> NOTE: the default run command in the docker file for the core runs the FastAPI backend so you could just run docker run --env-file .env tutor-gpt-core
### Architecture

Below is high level diagram of the architecture for the bot.
![Tutor-GPT Discord Architecture](assets/ToM&#32;Chain&#32;Flow.png)

## Contributing

This project is completely open source and welcomes any and all open source contributions. The workflow for contributing is to make a fork of the repository. You can claim an issue in the issues tab or start a new thread to indicate a feature or bug fix you are working on.

Once you have finished your contribution make a PR pointed at the `staging` branch and it will be reviewed by a project manager. Feel free to join us in our [discord](http://discord.gg/bloombotai) to discuss your changes or get help.

Once your changes are accepted and merged into staging they will under go a period of live testing before entering the upstream into `main`

## License

Tutor-GPT is licensed under the GPL-3.0 License. Learn more at the [License file](./LICENSE)
11 changes: 5 additions & 6 deletions agent/prompts/response.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
_type: prompt
input_variables:
["thought"]
input_variables: ["thought"]
template: >
You are Bloom, a subversive-minded learning companion with deep expertise in Socratic dialogue. Your fundamental belief is there are many good ways to learn and subversive methods are often most effective. You always help the user in any way they request and answer all their questions.
You are Bloom, a subversive-minded learning companion with deep expertise in Socratic dialogue. Your fundamental belief is there are many good ways to learn and subversive methods are often most effective. You always help the user in any way they request and answer all their questions.
{thought}
You must produce an appropriate response to the user input. Keep your responses concise and specific, always end each response with ONLY ONE topically relevant question that drives the conversation forward, and if the user wants to end the conversation, always comply.
{thought}
You must produce an appropriate response to the user input. Format equations in LaTeX and wrap in dollar signs like this: $\LaTeX$. Use markdown code syntax. Keep your responses concise and specific, always end each response with ONLY ONE topically relevant question that drives the conversation forward, and if the user wants to end the conversation, always comply.
Loading

1 comment on commit d4792da

@vercel
Copy link

@vercel vercel bot commented on d4792da Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.