cohere

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const EmbedModelMetadata = "cohere.embed.model"

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatResponse

type ChatResponse struct {
	ID           string `json:"id"`
	FinishReason string `json:"finish_reason"`
	Message      struct {
		Role    string `json:"role"`
		Content []struct {
			Type string `json:"type"`
			Text string `json:"text"`
		} `json:"content"`
	} `json:"message"`
}

type CommandProcessor added in v0.13.4

type CommandProcessor struct {
	sdk.UnimplementedProcessor
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"fmt"

	"github.com/conduitio/conduit-commons/config"
	"github.com/conduitio/conduit-commons/opencdc"
	sdk "github.com/conduitio/conduit-processor-sdk"
	"github.com/conduitio/conduit/pkg/foundation/log"
	"github.com/conduitio/conduit/pkg/plugin/processor/builtin/internal/exampleutil"
)

func main() {
	p := NewCommandProcessor(log.Nop())
	p.client = &mockCommandClient{}

	exampleutil.RunExample(p, exampleutil.Example{
		Summary: `Generate responses using Cohere's command model`,
		Description: `
This example demonstrates how to use the Cohere command processor to generate responses for a record's ` + "`.Payload.After`" + ` field.
The processor sends the input text from the configured "request.body" to the Cohere API and stores the model's response into the configured "response.body"`,
		Config: config.Config{
			commandProcessorConfigApiKey: "apikey",
			commandProcessorConfigPrompt: "hello",
		},
		Have: opencdc.Record{
			Operation: opencdc.OperationUpdate,
			Position:  opencdc.Position("pos-1"),
			Payload: opencdc.Change{
				After: opencdc.RawData("who are you?"),
			},
		},
		Want: sdk.SingleRecord{
			Operation: opencdc.OperationUpdate,
			Position:  opencdc.Position("pos-1"),
			Payload: opencdc.Change{
				After: opencdc.RawData("cohere command response content"),
			},
		},
	})

}

type mockCommandClient struct{}

func (m mockCommandClient) command(ctx context.Context, content string) (string, error) {
	if content == "" {
		return "", fmt.Errorf("mocked api error")
	}
	return "cohere command response content", nil
}
Output:

processor transformed record:
--- before
+++ after
@@ -1,10 +1,10 @@
 {
   "position": "cG9zLTE=",
   "operation": "update",
   "metadata": null,
   "key": null,
   "payload": {
     "before": null,
-    "after": "who are you?"
+    "after": "cohere command response content"
   }
 }

func NewCommandProcessor

func NewCommandProcessor(l log.CtxLogger) *CommandProcessor

func (*CommandProcessor) Configure added in v0.13.4

func (p *CommandProcessor) Configure(ctx context.Context, cfg config.Config) error

func (*CommandProcessor) Process added in v0.13.4

func (p *CommandProcessor) Process(ctx context.Context, records []opencdc.Record) []sdk.ProcessedRecord

func (*CommandProcessor) Specification added in v0.13.4

func (p *CommandProcessor) Specification() (sdk.Specification, error)

type EmbedProcessor added in v0.13.4

type EmbedProcessor struct {
	sdk.UnimplementedProcessor
	// contains filtered or unexported fields
}
Example
p := NewEmbedProcessor(log.Nop())
p.client = mockEmbedClient{}

embedding, err := p.client.embed(context.Background(), []string{"test input"})
if err != nil {
	panic(fmt.Sprintf("failed to get embedding: %v", err))
}
if len(embedding) == 0 {
	panic("no embeddings found")
}

embeddingJSON, err := json.Marshal(embedding[0])
if err != nil {
	panic(fmt.Sprintf("failed to marshal embeddings: %v", err))
}

// Compress the embedding using zstd
compressedEmbedding, err := compressData(embeddingJSON)
if err != nil {
	panic(fmt.Sprintf("failed to compress embeddings: %v", err))
}

exampleutil.RunExample(p, exampleutil.Example{
	Summary: `Generate embeddings using Cohere's embedding model`,
	Description: `
This example demonstrates how to use the Cohere embedding processor to generate embeddings for a record.
The processor extracts text from the configured "inputField" (default: ".Payload.After"), sends it to the Cohere API,
and stores the resulting embeddings in the configured "outputField" as compressed data using the zstd algorithm.

In this example, the processor is configured with a mock client and an API key. The input record's metadata is updated
to include the embedding model used ("embed-english-v2.0").`,
	Config: config.Config{
		"apiKey":      "fake-api-key",
		"inputField":  ".Payload.After",
		"outputField": ".Payload.After",
	},
	Have: opencdc.Record{
		Operation: opencdc.OperationCreate,
		Position:  opencdc.Position("pos-1"),
		Metadata:  map[string]string{},
		Payload: opencdc.Change{
			After: opencdc.RawData("test input"),
		},
	},
	Want: sdk.SingleRecord{
		Operation: opencdc.OperationCreate,
		Position:  opencdc.Position("pos-1"),
		Metadata:  opencdc.Metadata{"cohere.embed.model": "embed-english-v2.0"},
		Payload: opencdc.Change{
			After: opencdc.RawData(compressedEmbedding),
		},
	},
})
Output:

processor transformed record:
--- before
+++ after
@@ -1,10 +1,12 @@
 {
   "position": "cG9zLTE=",
   "operation": "create",
-  "metadata": {},
+  "metadata": {
+    "cohere.embed.model": "embed-english-v2.0"
+  },
   "key": null,
   "payload": {
     "before": null,
-    "after": "test input"
+    "after": "(\ufffd/\ufffd\u0004\u0000i\u0000\u0000[0.1,0.2,0.3]\ufffd^xH"
   }
 }

func NewEmbedProcessor

func NewEmbedProcessor(l log.CtxLogger) *EmbedProcessor

func (*EmbedProcessor) Configure added in v0.13.4

func (p *EmbedProcessor) Configure(ctx context.Context, cfg config.Config) error

func (*EmbedProcessor) Open added in v0.13.4

func (p *EmbedProcessor) Open(ctx context.Context) error

func (*EmbedProcessor) Process added in v0.13.4

func (p *EmbedProcessor) Process(ctx context.Context, records []opencdc.Record) []sdk.ProcessedRecord

func (*EmbedProcessor) Specification added in v0.13.4

func (p *EmbedProcessor) Specification() (sdk.Specification, error)

type RerankProcessor added in v0.13.4

type RerankProcessor struct {
	sdk.UnimplementedProcessor
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"fmt"
	"math"

	"github.com/conduitio/conduit-commons/config"
	"github.com/conduitio/conduit-commons/opencdc"
	sdk "github.com/conduitio/conduit-processor-sdk"
	"github.com/conduitio/conduit/pkg/foundation/log"
	"github.com/conduitio/conduit/pkg/plugin/processor/builtin/internal/exampleutil"
)

func main() {
	p := NewRerankProcessor(log.Nop())
	p.client = &mockRerankClient{}

	exampleutil.RunExample(p, exampleutil.Example{
		Summary: `Generate responses using Cohere's rerank model`,
		Description: `
This example demonstrates how to use the Cohere rerank processor.This takes in a query and a list of texts and produces an ordered 
array with each text assigned a relevance score. The processor extracts text from the configured "request.body" (default: ".Payload.After"), 
sends it to the Cohere API, and stores the response in the configured "response.body".

In this example, the processor is configured with a mock client and an API key. The input record's metadata is updated
to include the rerank model used ("rerank-v3.5").`,
		Config: config.Config{
			"apiKey": "fakeapiKey",
			"query":  "What is the capital of the United States?",
		},
		Have: opencdc.Record{
			Operation: opencdc.OperationUpdate,
			Position:  opencdc.Position("pos-1"),
			Payload: opencdc.Change{
				After: opencdc.RawData("Carson City is the capital city of the American state of Nevada."),
			},
		},
		Want: sdk.SingleRecord{
			Operation: opencdc.OperationUpdate,
			Position:  opencdc.Position("pos-1"),
			Payload: opencdc.Change{
				After: opencdc.RawData(`{"document":{"text":"Carson City is the capital city of the American state of Nevada."},"index":0,"relevance_score":0.9}`),
			},
		},
	})

}

type mockRerankClient struct{}

func (m mockRerankClient) rerank(ctx context.Context, docs []string) ([]RerankResult, error) {
	if len(docs) == 0 {
		return nil, fmt.Errorf("mocked api error")
	}
	result := make([]RerankResult, 0, len(docs))

	mockedScore := 1.0
	for i, d := range docs {
		mockedScore -= 0.1
		mockedScore = math.Round(mockedScore*10) / 10
		res := RerankResult{
			Index:          i,
			RelevanceScore: mockedScore,
		}
		res.Document.Text = d
		result = append(result, res)
	}

	return result, nil
}
Output:

processor transformed record:
--- before
+++ after
@@ -1,10 +1,10 @@
 {
   "position": "cG9zLTE=",
   "operation": "update",
   "metadata": null,
   "key": null,
   "payload": {
     "before": null,
-    "after": "Carson City is the capital city of the American state of Nevada."
+    "after": "{\"document\":{\"text\":\"Carson City is the capital city of the American state of Nevada.\"},\"index\":0,\"relevance_score\":0.9}"
   }
 }

func NewRerankProcessor

func NewRerankProcessor(l log.CtxLogger) *RerankProcessor

func (*RerankProcessor) Configure added in v0.13.4

func (p *RerankProcessor) Configure(ctx context.Context, cfg config.Config) error

func (*RerankProcessor) Process added in v0.13.4

func (p *RerankProcessor) Process(ctx context.Context, records []opencdc.Record) []sdk.ProcessedRecord

func (*RerankProcessor) Specification added in v0.13.4

func (p *RerankProcessor) Specification() (sdk.Specification, error)

type RerankResponse

type RerankResponse struct {
	ID      string         `json:"id"`
	Results []RerankResult `json:"results"`
}

type RerankResult

type RerankResult struct {
	Document struct {
		Text string `json:"text"`
	} `json:"document"`
	Index          int     `json:"index"`
	RelevanceScore float64 `json:"relevance_score"`
}

func (RerankResult) String

func (r RerankResult) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL