Struct dify_client::client::Client

source ·
pub struct Client {
    pub config: Arc<Config>,
    /* private fields */
}
Expand description

The Client struct represents a client for interacting with the Dify API.

Fields§

§config: Arc<Config>

The configuration for the client.

Implementations§

source§

impl Client

The Client struct represents a client for interacting with the Dify API.

source

pub fn new(base_url: &str, api_key: &str) -> Self

Creates a new Client instance with the specified base URL and API key.

§Arguments
  • base_url - The base URL of the Dify API.
  • api_key - The API key for authentication.
§Returns

A new Client instance.

source

pub fn new_with_config(c: Config) -> Self

Creates a new Client instance with the specified configuration.

§Arguments
  • c - The configuration for the client.
§Returns

A new Client instance.

source

pub async fn chat_messages( &self, req_data: ChatMessagesRequest ) -> Result<ChatMessagesResponse>

Sends a chat message request to the Dify API and returns the response.

§Arguments
  • req_data - The chat message request data.
§Returns

A Result containing the chat message response or an error.

source

pub async fn chat_messages_stream<F, T>( &self, req_data: ChatMessagesRequest, callback: F ) -> Result<Vec<T>>
where F: Fn(SteamMessageEvent) -> Result<Option<T>> + Send + Sync,

Sends a chat message request to the Dify API and returns the response as a stream. The callback function is called for each event in the stream. The callback function should return Some(T) if the event is processed successfully, otherwise None. The function returns a vector of the processed events. The stream is stopped when the callback function returns an error or the stream ends.

§Arguments
  • req_data - The chat message request data.
  • callback - The callback function to process the stream events.
§Returns

A Result containing the processed events or an error.

§Errors

Returns an error if the request cannot be created or the stream fails.

source

pub async fn files_upload( &self, req_data: FilesUploadRequest ) -> Result<FilesUploadResponse>

Sends a request to upload files to the Dify API and returns the response.
上传文件(目前仅支持图片)并在发送消息时使用,可实现图文多模态理解。
支持 png, jpg, jpeg, webp, gif 格式。
上传的文件仅供当前终端用户使用。

§Arguments
  • req_data - The files upload request data.
§Returns

A Result containing the files upload response or an error.

source

pub async fn chat_messages_stop( &self, req_data: StreamTaskStopRequest ) -> Result<ResultResponse>

Sends a request to stop stream chat messages to the Dify API and returns the response.

§Arguments
  • req_data - The chat message stop request data.
§Returns

A Result containing the chat message stop response or an error.

source

pub async fn messages_suggested( &self, req_data: MessagesSuggestedRequest ) -> Result<MessagesSuggestedResponse>

Sends a request to retrieve suggested messages from the Dify API and returns the response.

§Arguments
  • req_data - The messages suggested request data.
§Returns

A Result containing the messages suggested response or an error.

source

pub async fn messages_feedbacks( &self, req_data: MessagesFeedbacksRequest ) -> Result<ResultResponse>

Sends a request to retrieve messages feedbacks from the Dify API and returns the response.

§Arguments
  • req_data - The messages feedbacks request data.
§Returns

A Result containing the messages feedbacks response or an error.

source

pub async fn conversations( &self, req_data: ConversationsRequest ) -> Result<ConversationsResponse>

Sends a request to retrieve conversations from the Dify API and returns the response.

§Arguments
  • req_data - The conversations request data.
§Returns

A Result containing the conversations response or an error.

source

pub async fn messages( &self, req_data: MessagesRequest ) -> Result<MessagesResponse>

Sends a request to retrieve history messages from the Dify API and returns the response.

§Arguments
  • req_data - The messages request data.
§Returns

A Result containing the messages response or an error.

source

pub async fn conversations_renaming( &self, req_data: ConversationsRenameRequest ) -> Result<ResultResponse>

Sends a request to rename a conversation in the Dify API and returns the response.

§Arguments
  • req_data - The conversations rename request data.
§Returns

A Result containing the conversations rename response or an error.

source

pub async fn conversations_delete( &self, req_data: ConversationsDeleteRequest ) -> Result<()>

Sends a request to delete a conversation in the Dify API and returns the response.

§Arguments
  • req_data - The conversations delete request data.
§Returns

A Result containing the conversations delete response or an error.

source

pub async fn text_to_audio(&self, req_data: TextToAudioRequest) -> Result<Bytes>

Sends a request to convert audio to text in the Dify API and returns the response.

§Arguments
  • req_data - The audio to text request data.
§Returns

A Result containing the audio to text response or an error.

source

pub async fn audio_to_text( &self, req_data: AudioToTextRequest ) -> Result<AudioToTextResponse>

Sends a request to convert audio to text in the Dify API and returns the response.

§Arguments
  • req_data - The audio to text request data.
§Returns

A Result containing the audio to text response or an error.

source

pub async fn parameters( &self, req_data: ParametersRequest ) -> Result<ParametersResponse>

Sends a request to retrieve parameters from the Dify API and returns the response.

§Arguments
  • req_data - The parameters request data.
§Returns

A Result containing the parameters response or an error.

source

pub async fn meta(&self, req_data: MetaRequest) -> Result<MetaResponse>

Sends a request to retrieve meta information from the Dify API and returns the response.

§Arguments
  • req_data - The meta request data.
§Returns

A Result containing the meta response or an error.

source

pub async fn workflows_run( &self, req_data: WorkflowsRunRequest ) -> Result<WorkflowsRunResponse>

Sends a request to run workflows from the Dify API and returns the response.

§Arguments
  • req_data - The workflows run request data.
§Returns

A Result containing the workflows run response or an error.

source

pub async fn workflows_run_stream<F, T>( &self, req_data: WorkflowsRunRequest, callback: F ) -> Result<Vec<T>>
where F: Fn(SteamMessageEvent) -> Result<Option<T>> + Send + Sync,

Sends a request to run workflows from the Dify API and returns the response as a stream. The callback function is called for each event in the stream. The callback function should return Some(T) if the event is processed successfully, otherwise None. The function returns a vector of the processed events. The stream is stopped when the callback function returns an error or the stream ends.

§Arguments
  • req_data - The workflows run request data.
  • callback - The callback function to process the stream events.
§Returns

A Result containing the processed events or an error.

§Errors

Returns an error if the request cannot be created or the stream fails.

source

pub async fn workflows_stop( &self, req_data: StreamTaskStopRequest ) -> Result<ResultResponse>

Sends a request to stop stream workflows from the Dify API and returns the response.

§Arguments
  • req_data - The stream task stop request data.
§Returns

A Result containing the stream task stop response or an error.

source

pub async fn completion_messages( &self, req_data: CompletionMessagesRequest ) -> Result<CompletionMessagesResponse>

Sends a request to create completion messages from the Dify API and returns the response. 发送请求给文本生成型应用

§Arguments
  • req_data - The completion messages request data.
§Returns

A Result containing the completion messages response or an error.

source

pub async fn completion_messages_stream<F, T>( &self, req_data: CompletionMessagesRequest, callback: F ) -> Result<Vec<T>>
where F: Fn(SteamMessageEvent) -> Result<Option<T>> + Send + Sync,

Sends a request to create completion messages from the Dify API and returns the response as a stream. The callback function is called for each event in the stream. The callback function should return Some(T) if the event is processed successfully, otherwise None. The function returns a vector of the processed events. The stream is stopped when the callback function returns an error or the stream ends.

§Arguments
  • req_data - The completion messages request data.
  • callback - The callback function to process the stream events.
§Returns

A Result containing the processed events or an error.

§Errors

Returns an error if the request cannot be created or the stream fails.

source

pub async fn completion_messages_stop( &self, req_data: StreamTaskStopRequest ) -> Result<ResultResponse>

Sends a request to stop stream completion messages from the Dify API and returns the response. 文本生成停止响应

§Arguments
  • req_data - The stream task stop request data.
§Returns

A Result containing the stream task stop response or an error.

Trait Implementations§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Client

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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<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