-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(2665): send system message once in InferTypeName generation #2676
Closed
onyedikachi-david
wants to merge
3
commits into
tailcallhq:main
from
onyedikachi-david:refactor/infer-type-name-system-message
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c6c7e24
refactor(2665): send system message once in InferTypeName generation
onyedikachi-david 81bfcce
fix(llm): correct type mismatch in InferTypeName.ask_with_context mai…
onyedikachi-david a449e6c
Merge branch 'main' into refactor/infer-type-name-system-message
onyedikachi-david File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
#[derive(Default)] | ||
pub struct InferTypeName { | ||
secret: Option<String>, | ||
wizard: Option<Wizard<Question, Answer>>, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
|
@@ -75,25 +76,60 @@ | |
|
||
impl InferTypeName { | ||
pub fn new(secret: Option<String>) -> InferTypeName { | ||
Self { secret } | ||
Self { secret, wizard: None } | ||
} | ||
|
||
fn create_system_messages() -> Vec<ChatMessage> { | ||
vec![ | ||
ChatMessage::system( | ||
"Given the sample schema of a GraphQL type suggest 5 meaningful names for it.", | ||
), | ||
ChatMessage::system("The name should be concise and preferably a single word"), | ||
ChatMessage::system("Example Input:"), | ||
ChatMessage::system( | ||
serde_json::to_string_pretty(&Question { | ||
fields: vec![ | ||
("id".to_string(), "String".to_string()), | ||
("name".to_string(), "String".to_string()), | ||
("age".to_string(), "Int".to_string()), | ||
], | ||
}) | ||
.unwrap(), | ||
), | ||
ChatMessage::system("Example Output:"), | ||
ChatMessage::system( | ||
serde_json::to_string_pretty(&Answer { | ||
suggestions: vec![ | ||
"Person".into(), | ||
"Profile".into(), | ||
"Member".into(), | ||
"Individual".into(), | ||
"Contact".into(), | ||
], | ||
}) | ||
.unwrap(), | ||
), | ||
ChatMessage::system("Ensure the output is in valid JSON format"), | ||
ChatMessage::system("Do not add any additional text before or after the json"), | ||
] | ||
} | ||
|
||
pub async fn generate(&mut self, config: &Config) -> Result<HashMap<String, String>> { | ||
let secret = self.secret.as_ref().map(|s| s.to_owned()); | ||
|
||
let wizard: Wizard<Question, Answer> = Wizard::new(groq::LLAMA38192, secret); | ||
self.wizard = Some(Wizard::new(groq::LLAMA38192, secret)); | ||
|
||
let mut new_name_mappings: HashMap<String, String> = HashMap::new(); | ||
|
||
// removed root type from types. | ||
let types_to_be_processed = config | ||
.types | ||
.iter() | ||
.filter(|(type_name, _)| !config.is_root_operation_type(type_name)) | ||
.collect::<Vec<_>>(); | ||
|
||
let total = types_to_be_processed.len(); | ||
let system_messages = Self::create_system_messages(); | ||
|
||
for (i, (type_name, type_)) in types_to_be_processed.into_iter().enumerate() { | ||
// convert type to sdl format. | ||
let question = Question { | ||
fields: type_ | ||
.fields | ||
|
@@ -104,7 +140,7 @@ | |
|
||
let mut delay = 3; | ||
loop { | ||
let answer = wizard.ask(question.clone()).await; | ||
let answer = self.ask_with_context(&system_messages, &question).await; | ||
match answer { | ||
Ok(answer) => { | ||
let name = &answer.suggestions.join(", "); | ||
|
@@ -125,15 +161,10 @@ | |
total | ||
); | ||
|
||
// TODO: case where suggested names are already used, then extend the base | ||
// question with `suggest different names, we have already used following | ||
// names: [names list]` | ||
break; | ||
} | ||
Err(e) => { | ||
// TODO: log errors after certain number of retries. | ||
if let Error::GenAI(_) = e { | ||
// TODO: retry only when it's required. | ||
tracing::warn!( | ||
"Unable to retrieve a name for the type '{}'. Retrying in {}s", | ||
type_name, | ||
|
@@ -149,6 +180,19 @@ | |
|
||
Ok(new_name_mappings.into_iter().map(|(k, v)| (v, k)).collect()) | ||
} | ||
|
||
async fn ask_with_context( | ||
&self, | ||
system_messages: &[ChatMessage], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can remove |
||
question: &Question, | ||
) -> Result<Answer> { | ||
let content = serde_json::to_string(question)?; | ||
let mut messages = system_messages.to_vec(); | ||
messages.push(ChatMessage::user(content)); | ||
|
||
let request = ChatRequest::new(messages); | ||
self.wizard.as_ref().unwrap().ask(request).await | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why it's optional?