Creating and Deploying a Connector

This section will provide a rough guide for implementing a connector. There are two major parts of this process:

  1. Set up the connector and underlying data source, which requires:
    1. Configuring the connection between the connector and the data source
    2. Configuring the connection between the connector and the Chat API
    3. Deploying the connector
  2. Register and use your connector with Cohere to get grounded generations

We highly recommend using one of the authentication and authorization methods outlined in our “Connector Authentication” section.

Step 1: Set up the Connector and Data Source

Configure the connection between the connector and the data source:

The connector acts as a proxy between Chat API and the data source. The first step is to configure it to be able to connect to and search the underlying data source. Linking the connector with the data source typically requires setting up a service account in the data source, configuring the service account’s permissions, and copying appropriate credentials into the connector’s environment.

We have provided helpful example skeleton code in this github repository. Each of the quickstart connectors contains a detailed description of the necessary configuration in their README file. The skeleton code is a small example of how to connect to the data source. You will have to edit the code to make it applicable for your situation. You may, for example, customize the exact search query to better suit your needs.

The quickstart connector for Google Drive, for example, requires you to create an “application” in the Google Cloud Console and copy variables into the connector’s environment, like so:

GDRIVE_SERVICE_ACCOUNT_INFO=xxxx

Configure the connection between the connector and the Chat API

Once the connection between the connector and the data source is configured you will need to ensure the response and response structure of the service is compatible with the Chat API. The connector must also respond within 30 seconds for the results to be used in the final generation.

The request from the Chat API to the connector is a POST request that accepts a single query parameter in the body. The request URL must end with /search and contain no query parameters, like so:

1curl --request POST
2 --url 'https://connector-example.com/search'
3 --header 'Content-Type: application/json'
4 --data '{
5 "query": "How do I expense a meal?"
6 }'

The response from the connector should be a JSON object with a list of documents in the result field, like so:

Example Response JSON
1{
2 "results": [
3 {
4 "id": "expense_doc_1",
5 "title": "Expense Policies for external travel",
6 "text": "You can expense any work-related...",
7 "url": "https://internal-site.com/expensing"
8 "created_at": "2023-11-25T20:09:31Z",
9 },
10 {
11 "id": "expense_doc_2",
12 "title": "Expense Policies for internal travel",
13 "text": "You can expense any work-related...",
14 "created_at": "2023-11-25T20:09:31Z",
15 },...
16 ]
17}
Document Structure Recommendations

The structure of the object in the results field is fully flexible, meaning any fields are allowed. However, we strongly recommend the following to improve the final generations:

  • Keep documents under 300 words or less. Or add a text field which is the field that is truncated when prompt_truncation=true in the request.
  • Add a timestamp field to support temporal user queries.
  • Add an id field to allow identification of the relevant document.
  • Add a title field to allow the citations returned in the reply to be better-formatted.
  • Use excludes to exclude fields, such as the id, from the prompt.
  • Add a url field to allow the client to link to the document.

More details can be found in the documentation for chat.

Deploy the Connector

After the connector and data source are configured correctly, you will need to deploy the connector in a place that Cohere’s API can reach it. For now, this means an internet-facing service. It is important that you can provide Cohere with a URL to send a request to.

Once deployed, ensure that the connector can respond to requests from outside your network. Use the URL at which you made the connector available to register it with Cohere.

Step 2: Register and use your Connector with Cohere

After you’ve deployed the connector and verified it can respond to requests, it is time to register the connector with Cohere. You can do so by sending the following POST request to our API. You will need a Cohere API key, which you can generate here. Here’s an example request and related response:

1import cohere
2
3# initialize the Cohere Client with an API Key
4co = cohere.Client('YOUR_API_KEY')
5
6created_connector = co.create_connector(
7 name="Example connector",
8 url="https://connector-example.com/search",
9 )
Example Response JSON
1{
2 "connector": {
3 "id": "example-connector-kh3g8q",
4 "organization_id": "00000000-1111-2222-3333-444444444444",
5 "name": "Example Connector",
6 "url": "https://connector-example.com/search",
7 "created_at": "2023-09-21T15:45:39.097677Z",
8 "updated_at": "2023-09-21T16:12:28.999055Z"
9 }
10}

Make note of the id, as you will need it when you want to use it later to ground model-generated responses.

During registration, the API will attempt to query the connector to verify that it works as expected. If this step fails, ensure that the connector can respond to requests from outside your network, like so:

1curl --request POST
2 --url 'https://connector-example.com/search'
3 --header 'Content-Type: application/json'
4 --data '{
5 "query": "How do I expense a meal?"
6 }'

Use your Connector with the Chat API

In order to produce grounded generations, include your connector id in the connectors field in your chat request. Heres an example:

1import cohere
2co = cohere.Client('Your API key')
3response = co.chat(
4 message="What is the chemical formula for glucose?",
5 connectors=[{"id": "example_connector_id"}] # this is from the create step
6)

And here’s an example response:

Example Response JSON
1{
2 "text": "To expense a meal, you will need to follow the guidelines set by the IRS. Firstly, the meal must not be lavish or extravagant. The taxpayer or their employee must be present when food or beverages are consumed. The food or beverages must be provided to a business associate. Most meals are deductible at 50% of the cost. Entertainment expenses are not deductible.",
3 "citations": [
4 {
5 "start": 47,
6 "end": 73,
7 "text": "Expense Policy.",
8 "document_ids": [
9 "internal-docs_0"
10 ]
11 },
12 ],
13 "documents": [
14 {
15 "id": "internal-docs_0",
16 "snippet": "Writing off these expenses is a smart move, but how do you go about it?\n\nThe good news is that business meals are 50 percent deductible. This means that every time you take out a client for dinner and drinks, you get to write off half of the bill.\n\nHowever, you can not go around deducting meals indiscriminately. The IRS has guidelines and tests that help you correctly deduct your meals. These include:\n\nConsidering the business context, the meal must not be lavish or extravagant.\n\nThe taxpayer or an employee of the taxpayer is present at the meal.\n\nThe expense must be an ordinary and necessary expense, under the Internal Revenue Code (IRC) Section 162(a), which is incurred or paid to carry on a trade or business.\n\nFood and beverages must be purchased separately from entertainment in the case that the entertainment activity provides food and beverages.",
17 "title": "Expense Policies",
18 "url": "https://internal-site.com/expensing"
19 },
20 ],
21 "search_results": [
22 {
23 "search_query": {
24 "text": "expense a business meal",
25 ...
26 },
27 "document_ids": [
28 "internal-docs_0",
29 ...
30 ],
31 "connector": {
32 "id": "internal-docs"
33 }
34 }
35 ],
36 "search_queries": [
37 {
38 "text": "expense a business meal",
39 }
40 ],
41}