-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAI like 'response_format' (#279)
* add response format and conver functions * format * disable lopdf erro when pdf-extract is usde
- Loading branch information
1 parent
a244a77
commit 6c925d6
Showing
7 changed files
with
193 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
pub trait LangchainIntoOpenAI<T>: Sized { | ||
fn into_openai(self) -> T; | ||
} | ||
|
||
pub trait LangchainFromOpenAI<T>: Sized { | ||
fn from_openai(openai: T) -> Self; | ||
} | ||
|
||
pub trait OpenAiIntoLangchain<T>: Sized { | ||
fn into_langchain(self) -> T; | ||
} | ||
|
||
pub trait OpenAIFromLangchain<T>: Sized { | ||
fn from_langchain(langchain: T) -> Self; | ||
} | ||
|
||
impl<T, U> LangchainIntoOpenAI<U> for T | ||
where | ||
U: OpenAIFromLangchain<T>, | ||
{ | ||
fn into_openai(self) -> U { | ||
U::from_langchain(self) | ||
} | ||
} | ||
|
||
impl<T, U> OpenAiIntoLangchain<U> for T | ||
where | ||
U: LangchainFromOpenAI<T>, | ||
{ | ||
fn into_langchain(self) -> U { | ||
U::from_openai(self) | ||
} | ||
} | ||
|
||
// Try into and from OpenAI | ||
|
||
pub trait TryLangchainIntoOpenAI<T>: Sized { | ||
type Error; | ||
|
||
fn try_into_openai(self) -> Result<T, Self::Error>; | ||
} | ||
|
||
pub trait TryLangchainFromOpenAI<T>: Sized { | ||
type Error; | ||
|
||
fn try_from_openai(openai: T) -> Result<Self, Self::Error>; | ||
} | ||
|
||
pub trait TryOpenAiIntoLangchain<T>: Sized { | ||
type Error; | ||
|
||
fn try_into_langchain(self) -> Result<T, Self::Error>; | ||
} | ||
|
||
pub trait TryOpenAiFromLangchain<T>: Sized { | ||
type Error; | ||
|
||
fn try_from_langchain(langchain: T) -> Result<Self, Self::Error>; | ||
} | ||
|
||
impl<T, U> TryLangchainIntoOpenAI<U> for T | ||
where | ||
U: TryOpenAiFromLangchain<T>, | ||
{ | ||
type Error = U::Error; | ||
|
||
fn try_into_openai(self) -> Result<U, U::Error> { | ||
U::try_from_langchain(self) | ||
} | ||
} | ||
|
||
impl<T, U> TryOpenAiIntoLangchain<U> for T | ||
where | ||
U: TryLangchainFromOpenAI<T>, | ||
{ | ||
type Error = U::Error; | ||
|
||
fn try_into_langchain(self) -> Result<U, U::Error> { | ||
U::try_from_openai(self) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::schemas::convert::OpenAIFromLangchain; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum ResponseFormat { | ||
Text, | ||
JsonObject, | ||
JsonSchema { | ||
description: Option<String>, | ||
name: String, | ||
schema: Option<serde_json::Value>, | ||
strict: Option<bool>, | ||
}, | ||
} | ||
|
||
impl OpenAIFromLangchain<ResponseFormat> for async_openai::types::ResponseFormat { | ||
fn from_langchain(langchain: ResponseFormat) -> Self { | ||
match langchain { | ||
ResponseFormat::Text => async_openai::types::ResponseFormat::Text, | ||
ResponseFormat::JsonObject => async_openai::types::ResponseFormat::JsonObject, | ||
ResponseFormat::JsonSchema { | ||
name, | ||
description, | ||
schema, | ||
strict, | ||
} => async_openai::types::ResponseFormat::JsonSchema { | ||
json_schema: async_openai::types::ResponseFormatJsonSchema { | ||
name, | ||
description, | ||
schema, | ||
strict, | ||
}, | ||
}, | ||
} | ||
} | ||
} |
This file contains 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