Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svilupp committed Nov 26, 2024
1 parent 3e9e4f1 commit 380b4a9
Show file tree
Hide file tree
Showing 34 changed files with 500 additions and 1,417 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

## [0.66.0]

### Added
- Added a new `AnnotationMessage` type for keeping human-only information in the message changes. See `?annotate!` on how to use it.
- Added a new `ConversationMemory` type to enable long multi-turn conversations with a truncated memory of the conversation history. Truncation works in "batches" to not prevent caching. See `?ConversationMemory` and `get_last` for more information.


## [0.65.0]

### Breaking
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PromptingTools"
uuid = "670122d1-24a8-4d70-bfce-740807c42192"
authors = ["J S @svilupp and contributors"]
version = "0.65.0"
version = "0.66.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
6 changes: 6 additions & 0 deletions src/PromptingTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export SystemMessage, UserMessage, AIMessage, AnnotationMessage, issystemmessage
export ConversationMemory, get_last, last_message, last_output
# export UserMessage, UserMessageWithImages, SystemMessage, DataMessage # for debugging only

# export ConversationMemory
include("memory.jl")
# export annotate!
include("annotation.jl")


export aitemplates, AITemplate
include("templates.jl")

Expand Down
40 changes: 40 additions & 0 deletions src/annotation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
annotate!(messages::AbstractVector{<:AbstractMessage}, content; kwargs...)
annotate!(message::AbstractMessage, content; kwargs...)
Add an annotation message to a vector of messages or wrap a single message in a vector with an annotation.
The annotation is always inserted after any existing annotation messages.
# Arguments
- `messages`: Vector of messages or single message to annotate
- `content`: Content of the annotation
- `kwargs...`: Additional fields for the AnnotationMessage (extras, tags, comment)
# Returns
Vector{AbstractMessage} with the annotation message inserted
# Example
```julia
messages = [SystemMessage("Assistant"), UserMessage("Hello")]
annotate!(messages, "This is important"; tags=[:important], comment="For review")
```
"""
function annotate!(messages::AbstractVector{T}, content::AbstractString;
kwargs...) where {T <: AbstractMessage}
# Convert to Vector{AbstractMessage} if needed
messages_abstract = T == AbstractMessage ? messages :
convert(Vector{AbstractMessage}, messages)

# Find last annotation message index
last_anno_idx = findlast(isabstractannotationmessage, messages_abstract)
insert_idx = isnothing(last_anno_idx) ? 1 : last_anno_idx + 1

# Create and insert annotation message
anno = AnnotationMessage(; content = content, kwargs...)
insert!(messages_abstract, insert_idx, anno)
return messages_abstract
end

function annotate!(message::AbstractMessage, content::AbstractString; kwargs...)
return annotate!(AbstractMessage[message], content; kwargs...)
end
2 changes: 2 additions & 0 deletions src/llm_anthropic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function render(schema::AbstractAnthropicSchema,
for msg in messages_replaced
if issystemmessage(msg)
system = msg.content
elseif isabstractannotationmessage(msg)
continue
elseif isusermessage(msg) || isaimessage(msg)
content = msg.content
push!(conversation,
Expand Down
3 changes: 3 additions & 0 deletions src/llm_google.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function render(schema::AbstractGoogleSchema,

# replace any handlebar variables in the messages
for msg in messages_replaced
if isabstractannotationmessage(msg)
continue
end
push!(conversation,
Dict(
:role => role4render(schema, msg), :parts => [Dict("text" => msg.content)]))
Expand Down
3 changes: 3 additions & 0 deletions src/llm_ollama.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function render(schema::AbstractOllamaSchema,

# replace any handlebar variables in the messages
for msg in messages_replaced
if isabstractannotationmessage(msg)
continue
end
new_message = Dict{String, Any}(
"role" => role4render(schema, msg), "content" => msg.content)
## Special case for images
Expand Down
2 changes: 2 additions & 0 deletions src/llm_ollama_managed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ function render(schema::AbstractOllamaManagedSchema,
system = msg.content
elseif msg isa UserMessage
prompt = msg.content
elseif isabstractannotationmessage(msg)
continue
elseif msg isa UserMessageWithImages
error("Managed schema does not support UserMessageWithImages. Please use OpenAISchema instead.")
elseif msg isa AIMessage
Expand Down
2 changes: 2 additions & 0 deletions src/llm_openai.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ function render(schema::AbstractOpenAISchema,
content = msg.content isa AbstractString ? msg.content : string(msg.content)
Dict("role" => role4render(schema, msg), "content" => content,
"tool_call_id" => msg.tool_call_id)
elseif isabstractannotationmessage(msg)
continue
else
## Vanilla assistant message
Dict("role" => role4render(schema, msg),
Expand Down
4 changes: 4 additions & 0 deletions src/llm_shared.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ role4render(schema::AbstractPromptSchema, msg::UserMessageWithImages) = "user"
role4render(schema::AbstractPromptSchema, msg::AIMessage) = "assistant"
role4render(schema::AbstractPromptSchema, msg::AIToolRequest) = "assistant"
role4render(schema::AbstractPromptSchema, msg::ToolMessage) = "tool"
role4render(schema::AbstractPromptSchema, msg::AbstractAnnotationMessage) = "annotation"
"""
render(schema::NoSchema,
messages::Vector{<:AbstractMessage};
Expand Down Expand Up @@ -76,6 +77,9 @@ function render(schema::NoSchema,
count_system_msg += 1
# move to the front
pushfirst!(conversation, msg)
elseif isabstractannotationmessage(msg)
# Ignore annotation messages
continue
else
# Note: Ignores any DataMessage or other types for the prompt/conversation history
@warn "Unexpected message type: $(typeof(msg)). Skipping."
Expand Down
2 changes: 1 addition & 1 deletion src/llm_sharegpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end
function render(schema::AbstractShareGPTSchema, conv::AbstractVector{<:AbstractMessage})
Dict("conversations" => [Dict("from" => role4render(schema, msg),
"value" => msg.content)
for msg in conv])
for msg in conv if !isabstractannotationmessage(msg)])
end

### AI Functions
Expand Down
3 changes: 3 additions & 0 deletions src/llm_tracer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ end
function role4render(schema::AbstractTracerSchema, msg::AIMessage)
role4render(schema.schema, msg)
end
function role4render(schema::AbstractTracerSchema, msg::AbstractAnnotationMessage)
role4render(schema.schema, msg)
end
"""
render(tracer_schema::AbstractTracerSchema,
conv::AbstractVector{<:AbstractMessage}; kwargs...)
Expand Down
Loading

0 comments on commit 380b4a9

Please sign in to comment.