Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
malexw committed May 2, 2024
2 parents a8c15a5 + 6628eb9 commit 817f48f
Show file tree
Hide file tree
Showing 105 changed files with 2,812 additions and 1,270 deletions.
4 changes: 4 additions & 0 deletions .env-template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DATABASE_URL=postgresql+psycopg2://postgres:postgres@db:5432
# TOOLS
PYTHON_INTERPRETER_URL=http://localhost:8080
TAVILY_API_KEY=<API_KEY_HERE>
WOLFRAM_ALPHA_APP_ID=<APP_ID_HERE>

# DEPLOYMENTS
# To use the Toolkit, at least one deployment must be setup, the simplest
Expand All @@ -24,3 +25,6 @@ AZURE_CHAT_ENDPOINT_URL=<ENDPOINT URL>

# Experimental features
USE_EXPERIMENTAL_LANGCHAIN=False

# Community features
USE_COMMUNITY_FEATURES='True'
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ Remember that there are many ways to contribute other than writing code: writing
3. [Fork this repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)
4. Make your changes on your fork and make sure all your [commits are signed](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification)!
5. [Submit the fork as a Pull Request](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request-from-a-fork) pointing to the `main` branch this repository. A maintainer should comment and/or review your Pull Request within a few days. Although depending on the circumstances, it may take longer.


## How to add features

All features must be added to the `community` folder. That's where you'll find tools and deployments that are not managed by Codeowners.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
dev:
@docker compose watch
down:
@docker compose down
run-tests:
docker compose run --build backend poetry run pytest src/backend/tests/$(file)
run-community-tests:
docker compose run --build backend poetry run pytest src/community/tests/$(file)
attach:
@docker attach cohere-toolkit-backend-1
exec-backend:
Expand Down
87 changes: 71 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,30 @@ Toolkit is a collection of prebuilt components enabling users to quickly build a

## Quick start

### Deploying to Google Cloud Run

Before deploying to Google Cloud Run, you'll need a postgres database accessible to your Google Cloud Project, authenticated by a username and password. You'll be prompted for a `DATABASE_URL` before the container builds.
Try the default Toolkit application yourself by deploying it in a container locally. You will need to have [Docker](https://www.docker.com/products/docker-desktop/) and [Docker-compose >= 2.22](https://docs.docker.com/compose/install/) installed.

[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run)
```bash

### Deploying to Azure
docker run -e COHERE_API_KEY='>>YOUR_API_KEY<<' -p 8000:8000 -p 4000:4000 ghcr.io/cohere-ai/cohere-toolkit:latest

You can deploy Toolkit with one click to Microsoft Azure Platform:
```

[<img src="https://aka.ms/deploytoazurebutton" height="48px">](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fcohere-ai%2Fcohere-toolkit%2Fmain%2Fazuredeploy.json)
Go to localhost:4000 in your browser and start chatting with the model. This will use the model hosted on Cohere's platform. If you want to add your own tools or use another model, follow the instructions below to fork the repository.

### Building and running locally

Clone the repo and run

```bash
make setup
make first-run
```

Follow the instructions to configure the model - either AWS Sagemaker, Azure, or Cohere's platform. This can also be done by running `make setup` (See Option 2 below), which will help generate a file for you, or by manually creating a `.env` file and copying the contents of the provided `.env-template`. Then replacing the values with the correct ones.

<details>
<summary>Environment variables</summary>

### Cohere Platform
### Cohere Platform

- `COHERE_API_KEY`: If your application will interface with Cohere's API, you will need to supply an API key. Not required if using AWS Sagemaker or Azure.
Sign up at https://dashboard.cohere.com/ to create an API key.
Expand Down Expand Up @@ -75,6 +73,7 @@ Requirements:
- [Docker](https://www.docker.com/products/docker-desktop/)
- [Poetry](https://python-poetry.org/docs/#installation)
- [Docker-compose >= 2.22](https://docs.docker.com/compose/install/)
- [Postgres](https://www.postgresql.org/download/)

#### Option 1 - Install locally with Docker:

Expand Down Expand Up @@ -130,12 +129,35 @@ Components in this repo include:

## Deployment Guides

Looking to deploy the Toolkit to your preferred cloud service provider? See our guides below:
Looking to serve your application in production? Deploy the Toolkit to your preferred cloud provider by following our guides below:

### Other deployment options
- [Single Container Setup](docs/deployment_guides/single_container.md): Useful as a quickstart to run the Toolkit, or deploy to AWS on an EC2 instance.
- [AWS ECS Deployment](docs/deployment_guides/aws_ecs_single_container.md): Deploy the Toolkit single container to AWS ECS(Fargate).
- [AWS ECS Fargate Deployment](docs/deployment_guides/aws_ecs_single_container.md): Deploy the Toolkit single container to AWS ECS(Fargate).
- [AWS ECS EC2 Deployment](docs/deployment_guides/aws_ecs_single_container_ec2.md): Deploy the Toolkit single container to AWS ECS(EC2).
- [Google Cloud Platform](docs/deployment_guides/gcp_deployment.md): Help setup your Cloud SQL instance, then build, push and deploy backend+frontend containers to Cloud Run.

### Deploying to Azure

You can deploy Toolkit with one click to Microsoft Azure Platform:

[<img src="https://aka.ms/deploytoazurebutton" height="48px">](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fcohere-ai%2Fcohere-toolkit%2Fmain%2Fazuredeploy.json)

This deployment type uses Azure Container Instances to host the Toolkit.
After your deployment is complete click "Go to resource" button.
1) Check the logs to see if the container is running successfully:
- click on the "Containers" button on the left side of the screen
- click on the container name
- click on "Logs" tab to see the logs
2) Navigate to the "Overview" tab to see the FQDN of the container instance
3) Open the \<FQDN\>:4000 in your browser to access the Toolkit

### Deploying to Google Cloud Run

Before deploying to Google Cloud Run, you'll need a postgres database accessible to your Google Cloud Project, authenticated by a username and password. You'll be prompted for a `DATABASE_URL` before the container builds.

[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run)

## Setup for Development

### Setting up Poetry
Expand All @@ -155,6 +177,13 @@ poetry run black .
poetry run isort .
```

## Setting up the Environment Variables
**Please confirm that you have at least one configuration of the Cohere Platform, SageMaker or Azure.**

You have two methods to set up the environment variables:
1. Run `make setup` and follow the instructions to configure it.
2. Run `cp .env-template .env` and adjust the values in the `.env` file according to your situation.

### Setting up Your Local Database

The docker-compose file should spin up a local `db` container with a PostgreSQL server. The first time you setup this project, and whenever new migrations are added, you will need to run:
Expand Down Expand Up @@ -209,6 +238,11 @@ make migrate

### Troubleshooting

#### Community features are not accessible

Make sure you add `USE_COMMUNITY_FEATURES=True` to your .env file.


#### Multiple errors after running make dev for the first time

Make sure you run the following command before running make dev:
Expand All @@ -217,6 +251,19 @@ Make sure you run the following command before running make dev:
make migrate
```


#### Error: pg_config executable not found.

Make sure that all requirements including postgres are properly installed.

If you're using MacOS, run:
```bash
brew install postgresql
```

For other operating systems, you can check the [postgres documentation](https://www.postgresql.org/download/).


#### Debugging locally

To debug any of the backend logic while the Docker containers are running, you can run:
Expand All @@ -241,6 +288,14 @@ it will allow you to debug.

## Component Guides

### How to use community features

By default, the toolkit runs without community tools or deployments. If you want to enable them, add the following to the .env file or use `make setup` to set this variable:

```bash
USE_COMMUNITY_FEATURES=True
```

### How to add your own model deployment

A model deployment is a running version of one of the Cohere command models. The Toolkit currently supports the model deployments:
Expand All @@ -252,9 +307,9 @@ A model deployment is a running version of one of the Cohere command models. The
- SageMaker (model_deployments/sagemaker.py)
- This deployment option calls into your SageMaker deployment. To create a SageMaker endpoint [follow the steps here](https://docs.cohere.com/docs/amazon-sagemaker-setup-guide), alternatively [follow a command notebook here](https://github.com/cohere-ai/cohere-aws/tree/main/notebooks/sagemaker). Note your region and endpoint name when executing the notebook as these will be needed in the environment variables.
- To add your own deployment:
1. Create a deployment file, add it to [/model_deployments](https://github.com/cohere-ai/toolkit/tree/main/src/backend/chat/custom/model_deployments) folder, implement the function calls from `BaseDeployment` similar to the other deployments.
2. Add the deployment to [src/backend/config/deployments.py](https://github.com/cohere-ai/toolkit/blob/main/src/backend/config/deployments.py)
3. Add the option to [cli/main.py](https://github.com/cohere-ai/toolkit/blob/main/cli/main.py) and the environment variables required to the env template.
1. Create a deployment file, add it to [/community/model_deployments](https://github.com/cohere-ai/cohere-toolkit/tree/main/src/community/model_deployments) folder, implement the function calls from `BaseDeployment` similar to the other deployments.
2. Add the deployment to [src/community/config/deployments.py](https://github.com/cohere-ai/cohere-toolkit/blob/main/src/community/config/deployments.py)
3. Add the environment variables required to the env template.
- To add a Cohere private deployment, use the steps above copying the cohere platform implementation changing the base_url for your private deployment and add in custom auth steps.

### How to call the backend as an API
Expand All @@ -273,11 +328,11 @@ curl --location 'http://localhost:8000/chat-stream' \

### How to add your own chat interface

Currently the core chat interface is the Coral frontend. To add your own interface, take the steps above for call the backend as an API in your implementation and add it alongside `src/interfaces/coral_web`.
Currently the core chat interface is the Coral frontend. To add your own interface, take the steps above for call the backend as an API in your implementation and add it alongside `src/community/interfaces/`.

### How to add a connector to the Toolkit

If you have already created a [connector](https://docs.cohere.com/docs/connectors), it can be used in the toolkit with `ConnectorRetriever`. Add in your configuration and then add the definition in [config/tools.py](https://github.com/cohere-ai/toolkit/blob/main/src/backend/config/tools.py) similar to `Arxiv` implementation with the category `Category.DataLoader`. You can now use the Coral frontend and API with the connector.
If you have already created a [connector](https://docs.cohere.com/docs/connectors), it can be used in the toolkit with `ConnectorRetriever`. Add in your configuration and then add the definition in [community/config/tools.py](https://github.com/cohere-ai/cohere-toolkit/blob/main/src/community/config/tools.py) similar to `Arxiv` implementation with the category `Category.DataLoader`. You can now use the Coral frontend and API with the connector.

### How to set up web search with the Toolkit

Expand Down
Binary file removed banner.png
Binary file not shown.
Empty file added cli/__init__.py
Empty file.
90 changes: 42 additions & 48 deletions cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
import inquirer
from dotenv import set_key

from backend.config.deployments import (
AVAILABLE_MODEL_DEPLOYMENTS as MANAGED_DEPLOYMENTS_SETUP,
)
from community.config.deployments import (
AVAILABLE_MODEL_DEPLOYMENTS as COMMUNITY_DEPLOYMENTS_SETUP,
)
from community.config.tools import COMMUNITY_TOOLS_SETUP


class bcolors:
OKCYAN = "\033[96m"
Expand All @@ -13,12 +21,6 @@ class bcolors:
ENDC = "\033[0m"


"""
To add a new deployment, add a new DeploymentName enum and a new entry in the DEPLOYMENTS dictionary containing the secrets required for the deployment.
If the deployment requires a custom implementation, add a new key "custom_implementation" with the function that will be called to set up the deployment.
"""


class DeploymentName(StrEnum):
COHERE_PLATFORM = "Cohere Platform"
SAGE_MAKER = "SageMaker"
Expand Down Expand Up @@ -83,14 +85,22 @@ def database_url_prompt(secrets):


def deployment_prompt(secrets, configs):
for secret in configs["secrets"]:
if configs.get("custom_implementation"):
configs["custom_implementation"](secrets)
else:
value = inquirer.text(
f"Enter the value for {secret}", validate=lambda _, x: len(x) > 0
)
secrets[secret] = value
for secret in configs.env_vars:
value = inquirer.text(
f"Enter the value for {secret}", validate=lambda _, x: len(x) > 0
)
secrets[secret] = value


def community_tools_prompt(secrets):
print_styled(
"🏘️ We have some community tools that you can set up. These tools are not required for the Cohere Toolkit to run."
)
use_community_features = inquirer.confirm(
"Do you want to set up community features (tools and deployments)?"
)
secrets["USE_COMMUNITY_FEATURES"] = use_community_features
return use_community_features


def tool_prompt(secrets, name, configs):
Expand Down Expand Up @@ -129,23 +139,24 @@ def update_variable_prompt(_, variables_to_update):

def write_env_file(secrets):
for key, value in secrets.items():
print_styled(f"🔑 Setting {key} in {DOT_ENV_FILE_PATH} file.")
set_key(DOT_ENV_FILE_PATH, key, value)
set_key(DOT_ENV_FILE_PATH, key, str(value))


def select_deployments_prompt(_):
def select_deployments_prompt(deployments, _):
print_styled("🚀 Let's set up your deployments.", bcolors.MAGENTA)

deployments = inquirer.checkbox(
"Select the deployments you want to set up",
choices=[deployment.value for deployment in DeploymentName],
choices=[deployment.value for deployment in deployments.keys()],
default=["Cohere Platform"],
validate=lambda _, x: len(x) > 0,
)
return deployments


def wrap_up(deployments):
print_styled("✅ Your .env file has been set up.", bcolors.OKGREEN)

print_styled(
"🎉 You're all set up! You can now run 'make migrate' and 'make dev' to start the Cohere Toolkit. Make sure Docker is running.",
bcolors.OKGREEN,
Expand Down Expand Up @@ -222,29 +233,6 @@ def show_examples():
}


DEPLOYMENTS = {
DeploymentName.COHERE_PLATFORM: {
"secrets": [
"COHERE_API_KEY",
],
"custom_implementation": cohere_api_key_prompt,
},
DeploymentName.SAGE_MAKER: {
"secrets": [
"SAGE_MAKER_PROFILE_NAME",
"SAGE_MAKER_REGION_NAME",
"SAGE_MAKER_ENDPOINT_NAME",
],
},
DeploymentName.AZURE: {
"secrets": [
"AZURE_API_KEY",
"AZURE_CHAT_ENDPOINT_URL",
],
},
}


def start():
secrets = {}
print_styled(WELCOME_MESSAGE, bcolors.OKGREEN)
Expand All @@ -257,14 +245,22 @@ def start():
implementation(secrets)

# SET UP TOOLS
use_community_features = community_tools_prompt(secrets)
if use_community_features:
TOOLS.update(COMMUNITY_TOOLS_SETUP)

for name, configs in TOOLS.items():
tool_prompt(secrets, name, configs)

deployments = select_deployments_prompt(secrets)

# SET UP ENVIRONMENT FOR DEPLOYMENTS
for deployment in deployments:
deployment_prompt(secrets, DEPLOYMENTS[deployment])
all_deployments = MANAGED_DEPLOYMENTS_SETUP.copy()
if use_community_features:
all_deployments.update(COMMUNITY_DEPLOYMENTS_SETUP)

selected_deployments = select_deployments_prompt(all_deployments, secrets)

for deployment in selected_deployments:
deployment_prompt(secrets, all_deployments[deployment])

# SET UP .ENV FILE
write_env_file(secrets)
Expand All @@ -273,10 +269,8 @@ def start():
variables_to_update = review_variables_prompt(secrets)
update_variable_prompt(secrets, variables_to_update)

print_styled("✅ Your .env file has been set up.", bcolors.OKGREEN)

# WRAP UP
wrap_up(deployments)
wrap_up(selected_deployments)

# SHOW SOME EXAMPLES
show_examples()
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ services:
- __pycache__/
- alembic/
- data/
- action: sync
path: ./src/community
target: /workspace/src/community
ignore:
- __pycache__/
- alembic/
- data/
stdin_open: true
tty: true
ports:
Expand Down
7 changes: 6 additions & 1 deletion docker_scripts/env-defaults
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ DB_USER=${DB_USER:-}
DB_PASS=${DB_PASS:-}
DB_TEMPLATE=${DB_TEMPLATE:-template1}

DB_EXTENSION=${DB_EXTENSION:-}
DB_EXTENSION=${DB_EXTENSION:-}

# Defaults for the toolkit
export NEXT_PUBLIC_API_HOSTNAME=${NEXT_PUBLIC_API_HOSTNAME:-http://localhost:8000}
export PYTHON_INTERPRETER_URL=${PYTHON_INTERPRETER_URL:-http://localhost:8080}
export DATABASE_URL=${DATABASE_URL:-postgresql+psycopg2://postgre:postgre@localhost:5432/toolkit}

0 comments on commit 817f48f

Please sign in to comment.