Skip to content

Expose a way to customize the path to the chat completions endpoint #369

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions async-openai/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ use crate::{
/// Related guide: [Chat completions](https://platform.openai.com//docs/guides/text-generation)
pub struct Chat<'c, C: Config> {
client: &'c Client<C>,
path: String,
}

impl<'c, C: Config> Chat<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self { client }
Self {
client,
path: "/chat/completions".to_string(),
}
}

/// Change the path to the chat completions route.
pub fn with_path(mut self, path: impl Into<String>) -> Self {
self.path = path.into();
self
}

/// Creates a model response for the given chat conversation. Learn more in
Expand Down Expand Up @@ -52,7 +62,7 @@ impl<'c, C: Config> Chat<'c, C> {
));
}
}
self.client.post("/chat/completions", request).await
self.client.post(&self.path, request).await
}

/// Creates a completion for the chat message
Expand Down Expand Up @@ -83,6 +93,6 @@ impl<'c, C: Config> Chat<'c, C> {

request.stream = Some(true);
}
Ok(self.client.post_stream("/chat/completions", request).await)
Ok(self.client.post_stream(&self.path, request).await)
}
}