Skip to content

Commit

Permalink
Add Python SDK to docs (#761)
Browse files Browse the repository at this point in the history
* Add Python SDK to docs

* Add doc to sidebar
  • Loading branch information
neoxelox authored Jan 7, 2025
1 parent 5e22336 commit 2191258
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,22 @@ ${getRequestBodyContent()}

return (
<div className='flex flex-col gap-4'>
<Text.H5>You can use the Latitude API to run this document:</Text.H5>
<Text.H5>
To run this document programmatically, execute the following command:
</Text.H5>
<CodeBlock language='bash'>{apiCode.trim()}</CodeBlock>
<Text.H5>
Check out{' '}
<a
target='_blank'
href='https://docs.latitude.so/guides/prompt-manager/api-access'
>
<Text.H5 underline color='primary'>
our docs
</Text.H5>
</a>{' '}
for more details.
</Text.H5>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react'

import { HEAD_COMMIT } from '@latitude-data/core/browser'
import { CodeBlock, Text } from '@latitude-data/web-ui'

Expand Down Expand Up @@ -54,14 +52,17 @@ const result = await sdk.prompts.run('${documentPath}'${getRunOptions() ? `, ${g
return (
<div className='flex flex-col gap-4'>
<Text.H5>
To run this document programmatically, First install the SDK:
First, to run this document programmatically, install the SDK:
</Text.H5>
<CodeBlock language='bash'>npm install @latitude-data/sdk</CodeBlock>
<Text.H5>Then, use the following code to run the document:</Text.H5>
<CodeBlock language='typescript'>{sdkCode}</CodeBlock>
<Text.H5>
Check out{' '}
<a target='_blank' href='https://docs.latitude.so'>
<a
target='_blank'
href='https://docs.latitude.so/guides/sdk/typescript'
>
<Text.H5 underline color='primary'>
our docs
</Text.H5>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { HEAD_COMMIT } from '@latitude-data/core/browser'
import { CodeBlock, Text } from '@latitude-data/web-ui'

export function PythonUsage({
projectId,
commitUuid,
documentPath,
apiKey,
parameters,
}: {
projectId: number
commitUuid: string
documentPath: string
apiKey: string | undefined
parameters: Set<string>
}) {
const getParametersString = () => {
if (parameters.size === 0) return ''

const entries = Array.from(parameters).map((key) => `\t\t'${key}': ''`)

return `\tparameters={\n${entries.join(',\n')}\n\t}`
}

const getRunOptions = () => {
const options = []

if (commitUuid !== HEAD_COMMIT) {
options.push(`\tversion_uuid='${commitUuid}'`)
}

const parametersString = getParametersString()
if (parametersString) {
options.push(parametersString)
}

return options.length > 0 ? `\n${options.join(',\n')}\n` : ''
}

const sdkCode = `from latitude_sdk import Latitude, LatitudeOptions, RunPromptOptions
# Do not expose the API key in client-side code
sdk = Latitude('${apiKey ?? 'YOUR_API_KEY'}', LatitudeOptions(project_id=${projectId}))
result = await sdk.prompts.run('${documentPath}', RunPromptOptions(${getRunOptions()}))
`

return (
<div className='flex flex-col gap-4'>
<Text.H5>
First, to run this document programmatically, install the SDK:
</Text.H5>
<CodeBlock language='bash'>pip install latitude-sdk</CodeBlock>
<Text.H5>Then, use the following code to run the document:</Text.H5>
<CodeBlock language='python'>{sdkCode}</CodeBlock>
<Text.H5>
Check out{' '}
<a target='_blank' href='https://docs.latitude.so/guides/sdk/python'>
<Text.H5 underline color='primary'>
our docs
</Text.H5>
</a>{' '}
for more details.
</Text.H5>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
'use client'

import React, { useState } from 'react'
import { useState } from 'react'

import { ApiKey, DocumentVersion } from '@latitude-data/core/browser'
import { Tabs, type TabItem } from '@latitude-data/web-ui'

import { APIUsage } from './APIUsage'
import { JavascriptUsage } from './JavascriptUsage'
import { PythonUsage } from './PythonUsage'

const tabs: TabItem[] = [
{ id: 'sdk', label: 'Javascript' },
{ id: 'javascript', label: 'Javascript' },
{ id: 'python', label: 'Python' },
{ id: 'api', label: 'HTTP API' },
]

Expand All @@ -28,7 +30,7 @@ export function SettingsTabs({
apiKeys,
parameters,
}: SettingsTabsProps) {
const [activeTab, setActiveTab] = useState('sdk')
const [activeTab, setActiveTab] = useState(tabs[0]!.id)

return (
<Tabs
Expand All @@ -38,7 +40,7 @@ export function SettingsTabs({
>
{(activeTab) => (
<div className='p-6'>
{activeTab === 'sdk' && (
{activeTab === 'javascript' && (
<JavascriptUsage
apiKey={apiKeys[0]?.token}
projectId={projectId}
Expand All @@ -47,6 +49,15 @@ export function SettingsTabs({
parameters={parameters}
/>
)}
{activeTab === 'python' && (
<PythonUsage
apiKey={apiKeys[0]?.token}
projectId={projectId}
commitUuid={commitUuid}
documentPath={document.path}
parameters={parameters}
/>
)}
{activeTab === 'api' && (
<APIUsage
apiKey={apiKeys[0]?.token}
Expand Down
13 changes: 12 additions & 1 deletion docs/guides/cookbook/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ description: Examples of how to use Latitude

Here's a list of common examples of how to use Latitude. The list will grow with time as we keep adding more examples.

#### Typescript

- [Creating a prompt](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/typescript/create_prompt.ts)
- [Running a prompt](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/typescript/run_document.ts)
- [Chaining multiple calls with a growing context (chatbot)](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/typescript/evaluate_chat.ts)
- [Implementing a RAG workflow](/guides/cookbook/rag)
- [Pushing external logs to Latitude for evaluation](https://githujkjkjkjb.com/latitude-dev/latitude-llm/blob/main/examples/sdks/typescript/push_logs.ts)
- [Pushing external logs to Latitude for evaluation](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/typescript/push_logs.ts)
- [HITL evaluation](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/typescript/hitl_evaluation.ts)

#### Python

- [Creating a prompt](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/python/create_prompt.py)
- [Running a prompt](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/python/run_prompt.py)
- [Chaining multiple calls with a growing context (chatbot)](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/python/run_prompt.py)
- [Pushing external logs to Latitude for evaluation](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/python/upload_logs.py)
- [Triggering LLM as judge evaluations](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/python/evaluate_chat.py)
- [HITL evaluation](https://github.com/latitude-dev/latitude-llm/blob/main/examples/sdks/python/evaluate_chat.py)
2 changes: 1 addition & 1 deletion docs/guides/logs/upload-logs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ Once you've correctly configured your evaluation, just upload your logs as descr

### Upload logs from the SDK

You can push logs to Latitude with Latitude's SDK. Check out Latitude' SDK [`log` method](/guides/sdk/javascript-typescript-sdk#pushing-a-log-to-latitude).
You can push logs to Latitude with Latitude's SDK. Check out Latitude' SDK [`log` method](/guides/sdk/typescript#pushing-a-log-to-latitude).
100 changes: 100 additions & 0 deletions docs/guides/sdk/python.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
title: Python
description: Integrate Latitude's SDK into your Python project
---

Latitude's Python integration has the following main features:

- Interact with Latitude's prompt manager from code: create, update and delete prompts
- Run prompts with Latitude's high-performing gateway
- Trigger LLM as judge and human in the loop evaluations
- Programmatically push external logs to Latitude for evaluation and monitoring

## Installation

import Installation from '/snippets/sdks/python/installation.mdx'

<Installation />

## Getting Started

First, import the Latitude class from the SDK and initialize it with your API key:

```python
from latitude_sdk import Latitude, LatitudeOptions

sdk = Latitude('your-api-key-here', LatitudeOptions(
project_id=12345, # Optional, otherwise you have to provide it in each method
version_uuid='optional-version-uuid', # Optional, by default it targets the latest live version
))
```

## Examples

Check out our [cookbook](/guides/cookbook/overview#python) for more examples of how to use Latitude's SDK.

## Prompt Management

### Get or create a prompt

To get or create a prompt, use the `get_or_create` method:

```python
from latitude_sdk import GetOrCreatePromptOptions

await sdk.prompts.get_or_create('path/to/your/prompt', GetOrCreatePromptOptions(
project_id=12345, # Optional, if you did not provide it in the constructor
version_uuid='optional-version-uuid', # Optional, by default it targets the latest live version
prompt='Your prompt here', # Optional, this will be the contents of your prompt if it does not exist
))
```

### Run a prompt through Latitude Gateway

Latitude's Gateway is a high-performing gateway that proxies your LLM calls
between your application and the LLM provider. It includes some additional
features like automatic prompt caching based on content and prompt
configuration.

In order to run a prompt through Latitude's Gateway, use the `run` method:

```python
from latitude_sdk import RunPromptOptions

await sdk.prompts.run('path/to/your/prompt', RunPromptOptions(
project_id=12345, # Optional if you provided it in the constructor
version_uuid='optional-version-uuid', # Optional, by default it targets the latest live version
stream=False, # Optional, by default it's false
parameters={
# Any parameters your prompt expects
},
on_event=lambda event: print(event), # Handle events during execution
on_finished=lambda result: print(result), # Handle the final result
on_error=lambda error: print(error), # Handle any errors
))
```

## Log Management

### Pushing a log to Latitude

To create a log programmatically, use the `create` method:

```python
from latitude_sdk import CreateLogOptions, UserMessage

messages = [
UserMessage(content='Please tell me a joke about doctors'),
]

await sdk.logs.create('path/to/your/prompt', messages, CreateLogOptions(
project_id=12345, # Optional, if you did not provide it in the constructor
version_uuid='optional-version-uuid', # Optional, by default it targets the latest live version
response='assistant response',
))
```

<Note>
Message follows [OpenAI's
format](https://platform.openai.com/docs/guides/text-generation/building-prompts).
</Note>
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace Anthropic calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/aws-bedrock.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace AWS Bedrock calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/azure-openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace Azure OpenAI calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/cohere.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace Cohere calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/google-ai-platform.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace Google AI Platform calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/openai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace OpenAI calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/vercel-ai-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace Vercel AI SDK calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/sdk/tracing/vertex-ai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: How to trace Vertex AI calls with Latitude

## Installation

import Installation from '/snippets/installation.mdx'
import Installation from '/snippets/sdks/typescript/installation.mdx'

<Installation />

Expand Down
Loading

0 comments on commit 2191258

Please sign in to comment.