-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): restructure CodeGeneration, CompletionStream, ChatCom…
…pletionStream (#1998) * refactor(core): remove generate method from TextGeneration * refactor out TextGeneration struct * eliminate chat sub module * refactor: extract CompletionStream * restruct ChatCompletionStream <-> CompletionStream * restruct with CodeGeneration * handle output limitation inside of Stream traits
- Loading branch information
Showing
15 changed files
with
187 additions
and
237 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
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,79 @@ | ||
use std::sync::Arc; | ||
|
||
use async_stream::stream; | ||
use derive_builder::Builder; | ||
use futures::StreamExt; | ||
use tabby_common::languages::Language; | ||
|
||
use crate::{decoding::StopConditionFactory, CompletionOptionsBuilder, CompletionStream}; | ||
|
||
#[derive(Builder, Debug)] | ||
pub struct CodeGenerationOptions { | ||
#[builder(default = "1024")] | ||
pub max_input_length: usize, | ||
|
||
#[builder(default = "256")] | ||
pub max_decoding_tokens: i32, | ||
|
||
#[builder(default = "0.1")] | ||
pub sampling_temperature: f32, | ||
|
||
#[builder(default = "crate::default_seed()")] | ||
pub seed: u64, | ||
|
||
#[builder(default = "None")] | ||
pub language: Option<&'static Language>, | ||
} | ||
|
||
pub struct CodeGeneration { | ||
imp: Arc<dyn CompletionStream>, | ||
stop_condition_factory: StopConditionFactory, | ||
} | ||
|
||
impl CodeGeneration { | ||
pub fn new(imp: Arc<dyn CompletionStream>) -> Self { | ||
Self { | ||
imp, | ||
stop_condition_factory: StopConditionFactory::default(), | ||
} | ||
} | ||
} | ||
|
||
impl CodeGeneration { | ||
pub async fn generate(&self, prompt: &str, options: CodeGenerationOptions) -> String { | ||
let s = stream! { | ||
let mut text = String::new(); | ||
let mut stop_condition = self.stop_condition_factory.create( | ||
prompt, | ||
options.language, | ||
); | ||
|
||
let options = CompletionOptionsBuilder::default() | ||
.max_input_length(options.max_input_length) | ||
.max_decoding_tokens(options.max_decoding_tokens) | ||
.sampling_temperature(options.sampling_temperature) | ||
.seed(options.seed) | ||
.build() | ||
.expect("Failed to build completion options"); | ||
|
||
for await new_text in self.imp.generate(prompt, options).await { | ||
let (should_stop, stop_length) = stop_condition.should_stop(&new_text); | ||
text += &new_text; | ||
if should_stop { | ||
// stop condition matched against prompt + generated text. There's a chance that stop_length >= text.len(); | ||
let new_text_length = text.len().checked_sub(stop_length).unwrap_or_default(); | ||
text.truncate(new_text_length); | ||
break; | ||
} | ||
} | ||
|
||
yield text; | ||
}; | ||
|
||
if let Some(text) = Box::pin(s).into_future().await.0 { | ||
text | ||
} else { | ||
String::new() | ||
} | ||
} | ||
} |
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,19 @@ | ||
use async_trait::async_trait; | ||
use derive_builder::Builder; | ||
use futures::stream::BoxStream; | ||
|
||
#[derive(Builder, Debug)] | ||
pub struct CompletionOptions { | ||
pub max_input_length: usize, | ||
|
||
pub max_decoding_tokens: i32, | ||
|
||
pub sampling_temperature: f32, | ||
|
||
pub seed: u64, | ||
} | ||
|
||
#[async_trait] | ||
pub trait CompletionStream: Sync + Send { | ||
async fn generate(&self, prompt: &str, options: CompletionOptions) -> BoxStream<String>; | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.