-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Python SDK to docs * Add doc to sidebar
- Loading branch information
Showing
19 changed files
with
279 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...Uuid]/documents/[documentUuid]/_components/DocumentationModal/_components/PythonUsage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.