Skip to content

Commit

Permalink
community: add community folder (#53)
Browse files Browse the repository at this point in the history
* Community: add community folder

* hugging face + update docs

* fix hugging face

* lint
  • Loading branch information
lusmoura committed Apr 29, 2024
1 parent a7203f2 commit 1145eed
Show file tree
Hide file tree
Showing 31 changed files with 995 additions and 161 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.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ dev:
@docker compose watch
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
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ Go to localhost:4000 in your browser and start chatting with the model. This wil
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 @@ -216,6 +216,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 Down Expand Up @@ -261,6 +266,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 @@ -272,9 +285,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/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/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 @@ -293,11 +306,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/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
Empty file added cli/__init__.py
Empty file.
29 changes: 26 additions & 3 deletions cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import inquirer
from dotenv import set_key

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 Down Expand Up @@ -93,6 +98,17 @@ def deployment_prompt(secrets, configs):
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):
print_styled(
f"🛠️ If you want to enable {name}, set up the following secrets. Otherwise, press enter."
Expand Down Expand Up @@ -146,6 +162,8 @@ def select_deployments_prompt(_):


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 @@ -257,12 +275,19 @@ 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)

# SET UP ENVIRONMENT FOR DEPLOYMENTS
deployments = select_deployments_prompt(secrets)

# SET UP ENVIRONMENT FOR DEPLOYMENTS
if use_community_features:
DEPLOYMENTS.update(COMMUNITY_DEPLOYMENTS_SETUP)

for deployment in deployments:
deployment_prompt(secrets, DEPLOYMENTS[deployment])

Expand All @@ -273,8 +298,6 @@ 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)

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
9 changes: 5 additions & 4 deletions docs/custom_tool_guides/tool_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ There are three types of tools:

## Step 3: Implement the Tool

Add your tool implementation [here](https://github.com/cohere-ai/toolkit/tree/main/src/backend/tools) (please note that this link might change). The specific subfolder used will depend on the type of tool you're implementing.
Add your tool implementation [here](https://github.com/cohere-ai/toolkit/tree/main/src/community/tools) (please note that this link might change). The specific subfolder used will depend on the type of tool you're implementing.

If you need to install a new module to run your tool, execute the following command and run `make dev` again.

```bash
poetry add <MODULE>
poetry add <MODULE> --group community
```

If you're working on a File or Data Loader, follow the steps outlined in [Implementing a Retriever](#implementing-a-retriever).
Expand Down Expand Up @@ -133,7 +133,7 @@ class CalculatorFunctionTool(BaseFunctionTool):

## Step 4: Making Your Tool Available

To make your tool available, add its definition to the tools config [here](https://github.com/cohere-ai/toolkit/blob/main/src/backend/config/tools.py).
To make your tool available, add its definition to the tools config [here](https://github.com/cohere-ai/toolkit/blob/main/src/community/config/tools.py).

Start by adding the tool name to the `ToolName` enum found at the top of the file.

Expand All @@ -145,6 +145,7 @@ Next, include the tool configurations in the `AVAILABLE_TOOLS` list. The definit
- Is_visible: A boolean value indicating whether this function should be visible in the UI.
- Category: The type of tool.
- Description: A brief description of the tool.
- Env_vars: A list of secrets required by the tool.

Function tool with custom parameter definitions:

Expand Down Expand Up @@ -202,4 +203,4 @@ curl --location 'http://localhost:8000/chat-stream' \

## Step 6 (extra): Add Unit tests

If you would like to go above and beyond, it would be helpful to add some unit tests to ensure that your tool is working as expected. Create a file [here](https://github.com/cohere-ai/toolkit/tree/main/src/backend/tests/tools) and add a few cases.
If you would like to go above and beyond, it would be helpful to add some unit tests to ensure that your tool is working as expected. Create a file [here](https://github.com/cohere-ai/toolkit/tree/main/src/community/tests/tools) and add a few cases.

0 comments on commit 1145eed

Please sign in to comment.