MessageBlock

Enum MessageBlock 

Source
pub enum MessageBlock {
    Text {
        text: String,
    },
    Image {
        source: ImageSource,
    },
    ImageUrl {
        image_url: ImageUrl,
    },
}
Expand description

消息内容块

一条消息可以包含多个内容块,支持文本、图片等多模态内容

§示例

use llm_connector::types::MessageBlock;

// 文本块
let text = MessageBlock::text("Hello, world!");

// 图片块(Base64)
let image = MessageBlock::image_base64("image/jpeg", "base64_data...");

// 图片块(URL)
let image_url = MessageBlock::image_url("https://example.com/image.jpg");

Variants§

§

Text

文本块

Fields

§text: String
§

Image

图片块(Anthropic 格式)

Fields

§

ImageUrl

图片 URL 块(OpenAI 格式)

Fields

§image_url: ImageUrl

Implementations§

Source§

impl MessageBlock

Source

pub fn text(text: impl Into<String>) -> Self

创建文本块

§示例
use llm_connector::types::MessageBlock;

let block = MessageBlock::text("Hello, world!");
Source

pub fn image_base64( media_type: impl Into<String>, data: impl Into<String>, ) -> Self

创建 Base64 图片块(Anthropic 格式)

§参数
  • media_type: 媒体类型,如 “image/jpeg”, “image/png”
  • data: Base64 编码的图片数据
§示例
use llm_connector::types::MessageBlock;

let block = MessageBlock::image_base64(
    "image/jpeg",
    "iVBORw0KGgoAAAANSUhEUgA..."
);
Source

pub fn image_url_anthropic(url: impl Into<String>) -> Self

创建图片 URL 块(Anthropic 格式)

§示例
use llm_connector::types::MessageBlock;

let block = MessageBlock::image_url_anthropic("https://example.com/image.jpg");
Source

pub fn image_url(url: impl Into<String>) -> Self

创建图片 URL 块(OpenAI 格式)

§示例
use llm_connector::types::MessageBlock;

let block = MessageBlock::image_url("https://example.com/image.jpg");
Source

pub fn image_url_with_detail( url: impl Into<String>, detail: impl Into<String>, ) -> Self

创建图片 URL 块(OpenAI 格式,带 detail 参数)

§参数
  • url: 图片 URL
  • detail: 图片细节级别,可选值: “auto”, “low”, “high”
§示例
use llm_connector::types::MessageBlock;

let block = MessageBlock::image_url_with_detail(
    "https://example.com/image.jpg",
    "high"
);
Source

pub fn as_text(&self) -> Option<&str>

获取文本内容(如果是文本块)

§示例
use llm_connector::types::MessageBlock;

let block = MessageBlock::text("Hello");
assert_eq!(block.as_text(), Some("Hello"));

let image = MessageBlock::image_url("https://...");
assert_eq!(image.as_text(), None);
Source

pub fn is_text(&self) -> bool

判断是否为文本块

Source

pub fn is_image(&self) -> bool

判断是否为图片块

Trait Implementations§

Source§

impl Clone for MessageBlock

Source§

fn clone(&self) -> MessageBlock

Returns a duplicate 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 MessageBlock

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for MessageBlock

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for MessageBlock

Source§

fn eq(&self, other: &MessageBlock) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for MessageBlock

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for MessageBlock

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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