Skip to content
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

fix: anthropic documents in prompt #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading