Integrations

Cohere with Python SDK

Cohere with Python SDK

In this guide, we show you how to integrate your products with Cohere using Orquesta Python SDK.

For the longer blog article, see: Integrate Orquesta with Cohere using Python SDK

Step 1 - Install the SDK

pip install orquesta-sdk

Step 2 - Execute prompt

You can find your API Key in your workspace https://my.orquesta.dev/<workspace-name>/settings/developers

import time
import cohere
from orquesta_sdk import OrquestaClient, OrquestaClientOptions
from orquesta_sdk.helpers import orquesta_cohere_parameters_mapper
from orquesta_sdk.prompts import OrquestaPromptMetrics


# Initialize Orquesta client
from orquesta_sdk import OrquestaClient, OrquestaClientOptions

api_key = "ORQUESTA-API-KEY"

options = OrquestaClientOptions(
    api_key=api_key,
    ttl=3600
)

client = OrquestaClient(options)

# Query the prompt from Orquesta
prompt = client.prompts.query(
  key="data_completion", # key of prompt to retrieve
  context={ # the context for the specific prompt variant
    "environments": ["test"]
  },
  variables={  }
)

# Start time of the completion request
start_time = time.time()
print(f'Start time: {start_time}') 

co = cohere.Client('COHERE-API-KEY') # Insert your Cohere API key
completion = co.generate(
    **orquesta_cohere_parameters_mapper(prompt.value),
    model=prompt.value.get("model"),
    prompt=prompt.value.get('prompt'),
)

# End time of the completion request
end_time = time.time()
print(f'End time: {end_time}')

# Calculate the difference (latency) in milliseconds
latency = (end_time - start_time) * 1000
print(f'Latency is: {latency}')

Step 3 - Report analytics back to Orquesta

After each query, Orquesta generates a log with a Trace ID. Using the add_metrics() method, you can add additional information, such as the llm-response, metadata, latency, and economics

# Tokenize responses
prompt_tokenization = co.tokenize(prompt.value.get('prompt'))
completion_tokenization = co.tokenize(completion.generations[0].text)

prompt_tokens = len(prompt_tokenization.tokens)
completion_tokens = len(completion_tokenization.tokens)
total_tokens = prompt_tokens + completion_tokens

# Report the metrics back to Orquesta
metrics = OrquestaPromptMetrics(
    economics={
        "total_tokens": total_tokens,
        "completion_tokens": completion_tokens,
        "prompt_tokens": prompt_tokens,
    },
    llm_response=completion.generations[0].text,
    latency=latency,
    metadata={
        "finish_reason": completion.generations[0].finish_reason,
    },
)

prompt.add_metrics(metrics=metrics)

Cohere with Python SDK

In this guide, we show you how to integrate your products with Cohere using Orquesta Python SDK.

For the longer blog article, see: Integrate Orquesta with Cohere using Python SDK

Step 1 - Install the SDK

pip install orquesta-sdk

Step 2 - Execute prompt

You can find your API Key in your workspace https://my.orquesta.dev/<workspace-name>/settings/developers

import time
import cohere
from orquesta_sdk import OrquestaClient, OrquestaClientOptions
from orquesta_sdk.helpers import orquesta_cohere_parameters_mapper
from orquesta_sdk.prompts import OrquestaPromptMetrics


# Initialize Orquesta client
from orquesta_sdk import OrquestaClient, OrquestaClientOptions

api_key = "ORQUESTA-API-KEY"

options = OrquestaClientOptions(
    api_key=api_key,
    ttl=3600
)

client = OrquestaClient(options)

# Query the prompt from Orquesta
prompt = client.prompts.query(
  key="data_completion", # key of prompt to retrieve
  context={ # the context for the specific prompt variant
    "environments": ["test"]
  },
  variables={  }
)

# Start time of the completion request
start_time = time.time()
print(f'Start time: {start_time}') 

co = cohere.Client('COHERE-API-KEY') # Insert your Cohere API key
completion = co.generate(
    **orquesta_cohere_parameters_mapper(prompt.value),
    model=prompt.value.get("model"),
    prompt=prompt.value.get('prompt'),
)

# End time of the completion request
end_time = time.time()
print(f'End time: {end_time}')

# Calculate the difference (latency) in milliseconds
latency = (end_time - start_time) * 1000
print(f'Latency is: {latency}')

Step 3 - Report analytics back to Orquesta

After each query, Orquesta generates a log with a Trace ID. Using the add_metrics() method, you can add additional information, such as the llm-response, metadata, latency, and economics

# Tokenize responses
prompt_tokenization = co.tokenize(prompt.value.get('prompt'))
completion_tokenization = co.tokenize(completion.generations[0].text)

prompt_tokens = len(prompt_tokenization.tokens)
completion_tokens = len(completion_tokenization.tokens)
total_tokens = prompt_tokens + completion_tokens

# Report the metrics back to Orquesta
metrics = OrquestaPromptMetrics(
    economics={
        "total_tokens": total_tokens,
        "completion_tokens": completion_tokens,
        "prompt_tokens": prompt_tokens,
    },
    llm_response=completion.generations[0].text,
    latency=latency,
    metadata={
        "finish_reason": completion.generations[0].finish_reason,
    },
)

prompt.add_metrics(metrics=metrics)

Cohere with Python SDK

In this guide, we show you how to integrate your products with Cohere using Orquesta Python SDK.

For the longer blog article, see: Integrate Orquesta with Cohere using Python SDK

Step 1 - Install the SDK

pip install orquesta-sdk

Step 2 - Execute prompt

You can find your API Key in your workspace https://my.orquesta.dev/<workspace-name>/settings/developers

import time
import cohere
from orquesta_sdk import OrquestaClient, OrquestaClientOptions
from orquesta_sdk.helpers import orquesta_cohere_parameters_mapper
from orquesta_sdk.prompts import OrquestaPromptMetrics


# Initialize Orquesta client
from orquesta_sdk import OrquestaClient, OrquestaClientOptions

api_key = "ORQUESTA-API-KEY"

options = OrquestaClientOptions(
    api_key=api_key,
    ttl=3600
)

client = OrquestaClient(options)

# Query the prompt from Orquesta
prompt = client.prompts.query(
  key="data_completion", # key of prompt to retrieve
  context={ # the context for the specific prompt variant
    "environments": ["test"]
  },
  variables={  }
)

# Start time of the completion request
start_time = time.time()
print(f'Start time: {start_time}') 

co = cohere.Client('COHERE-API-KEY') # Insert your Cohere API key
completion = co.generate(
    **orquesta_cohere_parameters_mapper(prompt.value),
    model=prompt.value.get("model"),
    prompt=prompt.value.get('prompt'),
)

# End time of the completion request
end_time = time.time()
print(f'End time: {end_time}')

# Calculate the difference (latency) in milliseconds
latency = (end_time - start_time) * 1000
print(f'Latency is: {latency}')

Step 3 - Report analytics back to Orquesta

After each query, Orquesta generates a log with a Trace ID. Using the add_metrics() method, you can add additional information, such as the llm-response, metadata, latency, and economics

# Tokenize responses
prompt_tokenization = co.tokenize(prompt.value.get('prompt'))
completion_tokenization = co.tokenize(completion.generations[0].text)

prompt_tokens = len(prompt_tokenization.tokens)
completion_tokens = len(completion_tokenization.tokens)
total_tokens = prompt_tokens + completion_tokens

# Report the metrics back to Orquesta
metrics = OrquestaPromptMetrics(
    economics={
        "total_tokens": total_tokens,
        "completion_tokens": completion_tokens,
        "prompt_tokens": prompt_tokens,
    },
    llm_response=completion.generations[0].text,
    latency=latency,
    metadata={
        "finish_reason": completion.generations[0].finish_reason,
    },
)

prompt.add_metrics(metrics=metrics)

Start powering your SaaS with LLMs

Start

powering

your SaaS

with LLMs

Start powering your SaaS with LLMs