-
Notifications
You must be signed in to change notification settings - Fork 0
Add Prompts API #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Stephen Belanger (Qard)
wants to merge
1
commit into
main
Choose a base branch
from
prompts-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Prompts API #14
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ use crate::experiments::api::{ | |
| }; | ||
| use crate::experiments::{BaseExperimentInfo, ExperimentBuilder}; | ||
| use crate::log_queue::{LogQueue, LogQueueConfig}; | ||
| use crate::prompt::{PromptBuilder, PromptFetchRequest, PromptFetchResponse, PromptFetcher}; | ||
| use crate::span::SpanSubmitter; | ||
| use crate::types::{ParentSpanInfo, SpanPayload}; | ||
|
|
||
|
|
@@ -505,6 +506,13 @@ impl BraintrustClient { | |
| crate::span::SpanBuilder::new(submitter, token, org_id) | ||
| } | ||
|
|
||
| /// Create a prompt builder with an explicit API token. | ||
| /// | ||
| /// Use this if you already have the token and don't want to use the login state. | ||
| pub fn prompt_builder_with_credentials(&self, token: impl Into<String>) -> PromptBuilder<Self> { | ||
| PromptBuilder::new(Arc::new(self.clone()), token) | ||
| } | ||
|
|
||
| /// Perform login synchronously. | ||
| async fn perform_login(&self, api_key: &str, org_name: Option<&str>) -> Result<()> { | ||
| let login_url = self | ||
|
|
@@ -910,6 +918,64 @@ impl ExperimentComparisonFetcher for BraintrustClient { | |
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl PromptFetcher for BraintrustClient { | ||
| async fn fetch_prompt( | ||
| &self, | ||
| token: &str, | ||
| request: PromptFetchRequest, | ||
| ) -> Result<PromptFetchResponse> { | ||
| let mut url = self | ||
| .inner | ||
| .api_url | ||
| .join("v1/prompt") | ||
| .map_err(|e| BraintrustError::InvalidConfig(e.to_string()))?; | ||
|
|
||
| { | ||
| let mut query = url.query_pairs_mut(); | ||
| query.append_pair("slug", &request.slug); | ||
| if let Some(project_id) = &request.project_id { | ||
| query.append_pair("project_id", project_id); | ||
| } | ||
| if let Some(project_name) = &request.project_name { | ||
| query.append_pair("project_name", project_name); | ||
| } | ||
| if let Some(version) = &request.version { | ||
| query.append_pair("version", version); | ||
| } | ||
| if let Some(environment) = &request.environment { | ||
| query.append_pair("environment", environment); | ||
| } | ||
| } | ||
|
|
||
| let response = self | ||
| .inner | ||
| .http_client | ||
| .get(url) | ||
| .bearer_auth(token) | ||
| .send() | ||
| .await | ||
| .map_err(|e| BraintrustError::Network(e.to_string()))?; | ||
|
|
||
| if !response.status().is_success() { | ||
| let status = response.status(); | ||
| let body = response.text().await.unwrap_or_default(); | ||
| return Err(BraintrustError::Api { | ||
| status: status.as_u16(), | ||
| message: body, | ||
| }); | ||
| } | ||
|
|
||
| response | ||
| .json::<PromptFetchResponse>() | ||
| .await | ||
| .map_err(|e| BraintrustError::Api { | ||
| status: 200, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should pick a better status than 200 for the error. |
||
| message: format!("Failed to parse prompt response: {}", e), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| #[derive(Debug, Default)] | ||
| struct MutableSpanEvent { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can't publish a crate with git deps, so we'll need to fix this before that happens.