RepoCohereCoherepublished Oct 24, 2022seen 6d

cohere-ai/sandbox-conversant-lib

Python

Open original ↗

Captured source

source ↗
published Oct 24, 2022seen 6dcaptured 10hhttp 200method plain

cohere-ai/sandbox-conversant-lib

Description: Conversational AI tooling & personas built on Cohere's LLMs

Language: Python

License: MIT

Stars: 176

Forks: 31

Open issues: 12

Created: 2022-10-24T14:17:36Z

Pushed: 2023-08-14T23:10:55Z

Default branch: main

Fork: no

Archived: yes

README:

################################################################################
# ____ _ ____ _ _ #
# / ___|___ | |__ ___ _ __ ___ / ___| __ _ _ __ __| | |__ _____ __ #
# | | / _ \| '_ \ / _ \ '__/ _ \ \___ \ / _` | '_ \ / _` | '_ \ / _ \ \/ / #
# | |__| (_) | | | | __/ | | __/ ___) | (_| | | | | (_| | |_) | (_) >


### Running Your Own Streamlit Demo

Cohere uses Streamlit to create its demo applications. If you’re new to Streamlit, you can install it [here](https://docs.streamlit.io/library/get-started/installation) and read more about running Streamlit commands [here](https://docs.streamlit.io/library/get-started/main-concepts).

If you would like to modify this Streamlit demo locally, we strongly recommend forking this repository rather than installing it as a library from PyPI.

If you'd like to spin up your own instance of the Streamlit demo, you will first need a `COHERE_API_KEY`.
You can generate one by visiting [dashboard.cohere.ai](https://dashboard.cohere.ai/welcome/register?utm_source=github&utm_medium=content&utm_campaign=sandbox&utm_content=conversant).

#### Local Streamlit apps
If you plan to run the Streamlit app locally, you can add the key to `.streamlit/secrets.toml`:

COHERE_API_KEY = "YOUR_API_KEY_HERE"

When running locally, Streamlit will read the `secrets.toml` file and silently inject these values into the environment variables. Alternatively, you may directly set the API key as an environment variable by running the following command from the command line:

export COHERE_API_KEY = "YOUR_API_KEY_HERE"

Start the Streamlit app from the command line with the following command:

streamlit run conversant/demo/streamlit_example.py

#### Hosted Streamlit apps
If instead you would like to create a hosted Streamlit app, add your Cohere API key to Streamlit via [Secrets Management](https://docs.streamlit.io/streamlit-cloud/get-started/deploy-an-app/connect-to-data-sources/secrets-management). Add the following line as a Secret:

COHERE_API_KEY = "YOUR_API_KEY_HERE"

### Creating a Custom Persona
Once you have your own instance of the Streamlit app, you can begin experimenting with creating custom personas! Check out the `config.json` for each persona in [`conversant/personas`](https://github.com/cohere-ai/sandbox-conversant-lib/tree/main/conversant/personas) directory. You'll need to create a subfolder within this directory that corresponds to your new persona and add a `config.json` file.

As a note, we strongly recommend forking the `sandbox-conversant-lib` repository rather than installing it as a library from PyPI. When you create a new persona, use the `personas` directory in the cloned repository. The directory structure should look like this:

conversant/personas ├── fortune-teller │ └── config.json └── your-persona-name # new └── config.json

The config file should contain the following:
- `chatbot_config`:
- `max_context_examples`: The length of the chat history for the chatbot to use in reply.
- `avatar`: Optional emoji shortcode or URL to image as the chatbot's avatar. Defaults to 🤖.
- `client_config`: Parameters for [`co.generate()`](https://docs.cohere.ai/generate-reference)
- `chat_prompt_config`:
- `preamble`: Description of the persona.
- `example_separator`: A string that separates each example conversation.
- `headers`: A name for the `bot` and the `user`.
- `examples`: A few conversation examples (few-shot), or empty (zero-shot).

`conversant` will take care of the rest! As an example, check out [`fortune-teller/config.json`](https://github.com/cohere-ai/sandbox-conversant-lib/blob/main/conversant/personas/fortune-teller/config.json). When you launch the Streamlit app, the new persona will appear in the drop down menu.

#### Running the app with a subset of custom personas

If you would like to run the app with a subset of custom personas, it's possible to create a new directory that contains only the desired ones. This is analogous to the `conversant/personas` directory, and needs to have the same structure:

custom-personas ├── your-first-persona │ └── config.json └── your-second-persona └── config.json

After creating this directory, you'll need to tell the app where to look for it. In the demo Streamlit app (`streamlit_example.py`), one of the
first lines reads `CUSTOM_PERSONA_DIRECTORY = None`. Change this to specify the desired
persona directory, e.g. `CUSTOM_PERSONA_DIRECTORY = "/Users/yourname/custom-personas"`.

If this is unchanged, the app will default to using the directory that contains the
`conversant` demo personas.

#### Troubleshooting missing personas

If you do not see the new persona in the drop down menu, you may need to specify a
custom persona directory. Follow [the instructions above](#running-the-app-with-a-subset-of-custom-personas) to tell the app where to look for the personas.

### Editing a Persona on the Demo
You can also edit a persona on the Streamlit app!


### Usage

With `conversant`, you can create a chatbot powered by [Cohere](https://cohere.ai/)'s large language models with just the following code snippet.

import cohere import conversant

co = cohere.Client("YOUR_API_KEY_HERE") bot = conversant.PromptChatbot.from_persona("fantasy-wizard", client=co) print(bot.reply("Hello!")) >>> "Well met, fair traveller. What bringest thou to mine village?"

You can also define your own persona by passing in your own `ChatPrompt`.

from conversant.prompts import ChatPrompt

shakespeare_config = { "preamble": "Below is a conversation between Shakespeare and a Literature Student.", "example_separator": "\n", "headers": { "user": "Literature Student", "bot": "William Shakespeare", }, "examples": [ [ { "user": "Who are you?", "bot": "Mine own nameth is Shakespeare, and I speaketh in riddles.", }, ] ], } shakespeare_bot = conversant.PromptChatbot( client=co, prompt=ChatPrompt.from_dict(shakespeare_config) ) print(shakespeare_bot.reply("Hello!")) >>> "Greeteth, and welcome. I am Shakespeare, the great poet, dramatist, and playwright."

## How Conversant Works
`conversant` uses prompt completion to define a chatbot persona with a description and a few…

Excerpt shown — open the source for the full document.