Skip to content

Commit

Permalink
fix: anthropic documents in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
milancermak committed Jan 8, 2025
1 parent 9cc7ed3 commit 0466515
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions rig-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- *(openai)* Make integration more general (#156)
- *(anthropic)* Properly format context documents in prompt (#183)

### Other

Expand Down
15 changes: 14 additions & 1 deletion rig-core/src/providers/anthropic/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,20 @@ impl completion::CompletionModel for CompletionModel {
// specific requirements of each provider. For now, we just manually check while
// building the request as a raw JSON document.

let prompt_with_context = completion_request.prompt_with_context();
let prompt_with_context = if completion_request.documents.is_empty() {
completion_request.prompt.clone()
} else {
let documents = completion_request.documents.into_iter().enumerate().map(|(i, doc)| {
format!(
"<document index=\"{}\"><source>{}</source><document_content>{}</document_content></document>",
i, doc.id, doc.text
)
}).collect::<Vec<_>>().join("\n");
format!(
"<documents>\n{documents}</documents>\n{}",
completion_request.prompt
)
};

// Check if max_tokens is set, required for Anthropic
let max_tokens = if let Some(tokens) = completion_request.max_tokens {
Expand Down

0 comments on commit 0466515

Please sign in to comment.