Struct OpenAIChatCompletionClient

Source
pub struct OpenAIChatCompletionClient { /* private fields */ }
Expand description

§OpenAIChatCompletionClient

Allows for interacting with open ai models

§Examples

use rag_toolchain::common::*;
use rag_toolchain::clients::*;
use serde_json::Map;
use serde_json::Value;
async fn generate_completion() {
    let model: OpenAIModel = OpenAIModel::Gpt3Point5Turbo;
    let mut additional_config: Map<String, Value> = Map::new();
    additional_config.insert("temperature".into(), 0.5.into());

    let client: OpenAIChatCompletionClient =
        OpenAIChatCompletionClient::try_new_with_additional_config(model, additional_config).unwrap();
    let system_message: PromptMessage = PromptMessage::SystemMessage(
        "You are a comedian that cant ever reply to someone unless its phrased as a sarcastic joke"
        .into());
    let user_message: PromptMessage =
    PromptMessage::HumanMessage("What is the weather like today ?".into());
    let reply = client
        .invoke(vec![system_message, user_message])
        .await
        .unwrap();
    println!("{:?}", reply.content());
}

§Required Environment Variables

OPENAI_API_KEY: The API key to use for the OpenAI API

Implementations§

Source§

impl OpenAIChatCompletionClient

Source

pub fn try_new( model: OpenAIModel, ) -> Result<OpenAIChatCompletionClient, VarError>

§OpenAIChatCompletionClient::try_new

This method creates a new OpenAIChatCompletionClient. All inference parameters used will be the default ones provided by the OpenAI API.

§Arguments
  • model: OpenAIModel - The model to use for the chat completion.
§Errors
  • VarError - if the OPENAI_API_KEY environment variable is not set.
§Returns
Source

pub fn try_new_with_additional_config( model: OpenAIModel, additional_config: Map<String, Value>, ) -> Result<OpenAIChatCompletionClient, VarError>

§OpenAIChatCompletionClient::try_new_with_additional_config

This method creates a new OpenAIChatCompletionClient. All inference parameters provided in the additional_config will be used in the chat completion request. an example of this could be ‘temperature’, ‘top_p’, ‘seed’ etc.

§Forbidden Properties
  • “stream”: this cannot be set as it is used internally by the client.
  • “n”: n can be set but will result in wasted tokens as the client is built for single chat completions. We intend to add support for multiple completions in the future.
§Arguments
  • model: OpenAIModel - The model to use for the chat completion.
  • additional_config: Map<String, Value> - The additional configuration to use for the chat completion.
§Errors
  • VarError - if the OPENAI_API_KEY environment variable is not set.
§Returns
Source

pub fn try_new_with_url( model: OpenAIModel, url: String, ) -> Result<OpenAIChatCompletionClient, VarError>

§OpenAIChatCompletionClient::try_new_with_url

This method creates a new OpenAIChatCompletionClient. All inference parameters used will be the default ones provided by the OpenAI API. You can pass the url in directly

§Arguments
  • model: OpenAIModel - The model to use for the chat completion.
  • url: String - The url to use for the api call.
§Errors
  • VarError - if the OPENAI_API_KEY environment variable is not set.
§Returns
Source

pub fn try_new_with_url_and_additional_config( model: OpenAIModel, url: String, additional_config: Map<String, Value>, ) -> Result<OpenAIChatCompletionClient, VarError>

§OpenAIChatCompletionClient::try_new_with_url_and_additional_config

This method creates a new OpenAIChatCompletionClient. All inference parameters provided in the additional_config will be used in the chat completion request. an example of this could be ‘temperature’, ‘top_p’, ‘seed’ etc. You can pass the url in directly.

§Forbidden Properties
  • “stream”: this cannot be set as it is used internally by the client.
  • “n”: n can be set but will result in wasted tokens as the client is built for single chat completions. We intend to add support for multiple completions in the future.
§Arguments
  • model: OpenAIModel - The model to use for the chat completion.
  • url: String - The url to use for the api call.
  • additional_config: Map<String, Value> - The additional configuration to use for the chat completion.
§Errors
  • VarError - if the OPENAI_API_KEY environment variable is not set.
§Returns

Trait Implementations§

Source§

impl AsyncChatClient for OpenAIChatCompletionClient

Source§

async fn invoke( &self, prompt_messages: Vec<PromptMessage>, ) -> Result<PromptMessage, Self::ErrorType>

§OpenAIChatCompletionClient::invoke

function to execute the ChatCompletion given a list of prompt messages.

§Arguments
  • prompt_messages: Vec<PromptMessage> - the list of prompt messages that will be sent to the LLM.
§Errors
§Returns

PromptMessage::AIMessage - the response from the chat client.

Source§

type ErrorType = OpenAIError

Source§

impl AsyncStreamedChatClient for OpenAIChatCompletionClient

Source§

async fn invoke_stream( &self, prompt_messages: Vec<PromptMessage>, ) -> Result<Self::Item, Self::ErrorType>

§OpenAIChatCompletionClient::invoke_stream

function to execute the ChatCompletion given a list of prompt messages.

§Arguments
  • prompt_messages: PromptMessage - the list of prompt messages that will be sent to the LLM.
§Errors
§Returns

impl ChatCompletionStream - the response from the chat client.

Source§

type ErrorType = OpenAIError

Source§

type Item = OpenAICompletionStream

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,