2024-03-18 15:48:46 -04:00
|
|
|
# frozen_string_literal: true
|
2024-04-03 01:06:27 -04:00
|
|
|
require_relative "endpoint_compliance"
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic do
|
|
|
|
let(:llm) { DiscourseAi::Completions::Llm.proxy("anthropic:claude-3-opus") }
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
let(:image100x100) { plugin_file_from_fixtures("100x100.jpg") }
|
|
|
|
let(:upload100x100) do
|
|
|
|
UploadCreator.new(image100x100, "image.jpg").create_for(Discourse.system_user.id)
|
|
|
|
end
|
2024-03-18 15:48:46 -04:00
|
|
|
|
|
|
|
let(:prompt) do
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
"You are hello bot",
|
|
|
|
messages: [type: :user, id: "user1", content: "hello"],
|
|
|
|
)
|
|
|
|
end
|
2024-01-02 09:21:13 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
let(:echo_tool) do
|
2023-11-23 10:58:54 -05:00
|
|
|
{
|
2024-03-18 15:48:46 -04:00
|
|
|
name: "echo",
|
|
|
|
description: "echo something",
|
|
|
|
parameters: [{ name: "text", type: "string", description: "text to echo", required: true }],
|
2023-11-23 10:58:54 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
let(:google_tool) do
|
|
|
|
{
|
|
|
|
name: "google",
|
|
|
|
description: "google something",
|
|
|
|
parameters: [
|
|
|
|
{ name: "query", type: "string", description: "text to google", required: true },
|
|
|
|
],
|
|
|
|
}
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
let(:prompt_with_echo_tool) do
|
|
|
|
prompt_with_tools = prompt
|
|
|
|
prompt.tools = [echo_tool]
|
|
|
|
prompt_with_tools
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
let(:prompt_with_google_tool) do
|
|
|
|
prompt_with_tools = prompt
|
|
|
|
prompt.tools = [echo_tool]
|
|
|
|
prompt_with_tools
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
2024-01-17 13:08:49 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
before { SiteSetting.ai_anthropic_api_key = "123" }
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
it "does not eat spaces with tool calls" do
|
2024-06-07 09:52:01 -04:00
|
|
|
SiteSetting.ai_anthropic_native_tool_call_models = "claude-3-opus"
|
2024-03-18 15:48:46 -04:00
|
|
|
body = <<~STRING
|
2024-06-05 18:34:23 -04:00
|
|
|
event: message_start
|
|
|
|
data: {"type":"message_start","message":{"id":"msg_01Ju4j2MiGQb9KV9EEQ522Y3","type":"message","role":"assistant","model":"claude-3-haiku-20240307","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":1293,"output_tokens":1}} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_start
|
|
|
|
data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_01DjrShFRRHp9SnHYRFRc53F","name":"search","input":{}} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: ping
|
|
|
|
data: {"type": "ping"}
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\\"searc"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"h_qu"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"er"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"y\\": \\"s"} }
|
2024-01-17 13:08:49 -05:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"am"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":" "} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"sam\\""} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":", \\"cate"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"gory"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"\\": \\"gene"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_delta
|
|
|
|
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"ral\\"}"} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: content_block_stop
|
|
|
|
data: {"type":"content_block_stop","index":0 }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: message_delta
|
|
|
|
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":70} }
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
event: message_stop
|
|
|
|
data: {"type":"message_stop"}
|
2024-03-18 15:48:46 -04:00
|
|
|
STRING
|
|
|
|
|
|
|
|
result = +""
|
2024-04-03 01:06:27 -04:00
|
|
|
body = body.scan(/.*\n/)
|
|
|
|
EndpointMock.with_chunk_array_support do
|
|
|
|
stub_request(:post, "https://api.anthropic.com/v1/messages").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: body,
|
|
|
|
)
|
|
|
|
|
|
|
|
llm.generate(prompt_with_google_tool, user: Discourse.system_user) do |partial|
|
|
|
|
result << partial
|
|
|
|
end
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
2024-03-18 15:48:46 -04:00
|
|
|
|
|
|
|
expected = (<<~TEXT).strip
|
|
|
|
<function_calls>
|
|
|
|
<invoke>
|
2024-06-05 18:34:23 -04:00
|
|
|
<tool_name>search</tool_name>
|
|
|
|
<parameters><search_query>sam sam</search_query>
|
|
|
|
<category>general</category></parameters>
|
|
|
|
<tool_id>toolu_01DjrShFRRHp9SnHYRFRc53F</tool_id>
|
2024-03-18 15:48:46 -04:00
|
|
|
</invoke>
|
|
|
|
</function_calls>
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
expect(result.strip).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can stream a response" do
|
|
|
|
body = (<<~STRING).strip
|
|
|
|
event: message_start
|
|
|
|
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}
|
|
|
|
|
|
|
|
event: content_block_start
|
|
|
|
data: {"type": "content_block_start", "index":0, "content_block": {"type": "text", "text": ""}}
|
|
|
|
|
|
|
|
event: ping
|
|
|
|
data: {"type": "ping"}
|
|
|
|
|
|
|
|
event: content_block_delta
|
|
|
|
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}
|
|
|
|
|
|
|
|
event: content_block_delta
|
|
|
|
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
|
|
|
|
|
|
|
|
event: content_block_stop
|
|
|
|
data: {"type": "content_block_stop", "index": 0}
|
|
|
|
|
|
|
|
event: message_delta
|
|
|
|
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}
|
|
|
|
|
|
|
|
event: message_stop
|
|
|
|
data: {"type": "message_stop"}
|
|
|
|
STRING
|
|
|
|
|
|
|
|
parsed_body = nil
|
|
|
|
|
|
|
|
stub_request(:post, "https://api.anthropic.com/v1/messages").with(
|
|
|
|
body:
|
|
|
|
proc do |req_body|
|
|
|
|
parsed_body = JSON.parse(req_body, symbolize_names: true)
|
|
|
|
true
|
|
|
|
end,
|
|
|
|
headers: {
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"X-Api-Key" => "123",
|
|
|
|
"Anthropic-Version" => "2023-06-01",
|
|
|
|
},
|
|
|
|
).to_return(status: 200, body: body)
|
|
|
|
|
|
|
|
result = +""
|
2024-05-13 23:28:46 -04:00
|
|
|
llm.generate(prompt, user: Discourse.system_user, feature_name: "testing") do |partial, cancel|
|
|
|
|
result << partial
|
|
|
|
end
|
2024-03-18 15:48:46 -04:00
|
|
|
|
|
|
|
expect(result).to eq("Hello!")
|
|
|
|
|
|
|
|
expected_body = {
|
|
|
|
model: "claude-3-opus-20240229",
|
|
|
|
max_tokens: 3000,
|
|
|
|
messages: [{ role: "user", content: "user1: hello" }],
|
|
|
|
system: "You are hello bot",
|
|
|
|
stream: true,
|
|
|
|
}
|
|
|
|
expect(parsed_body).to eq(expected_body)
|
|
|
|
|
|
|
|
log = AiApiAuditLog.order(:id).last
|
|
|
|
expect(log.provider_id).to eq(AiApiAuditLog::Provider::Anthropic)
|
2024-05-13 23:28:46 -04:00
|
|
|
expect(log.feature_name).to eq("testing")
|
2024-06-05 18:34:23 -04:00
|
|
|
expect(log.response_tokens).to eq(15)
|
|
|
|
expect(log.request_tokens).to eq(25)
|
2024-03-18 15:48:46 -04:00
|
|
|
end
|
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
it "supports non streaming tool calls" do
|
|
|
|
tool = {
|
|
|
|
name: "calculate",
|
|
|
|
description: "calculate something",
|
|
|
|
parameters: [
|
|
|
|
{
|
|
|
|
name: "expression",
|
|
|
|
type: "string",
|
|
|
|
description: "expression to calculate",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
"You a calculator",
|
|
|
|
messages: [{ type: :user, id: "user1", content: "calculate 2758975 + 21.11" }],
|
|
|
|
tools: [tool],
|
|
|
|
)
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
proxy = DiscourseAi::Completions::Llm.proxy("anthropic:claude-3-haiku")
|
|
|
|
|
|
|
|
body = {
|
|
|
|
id: "msg_01RdJkxCbsEj9VFyFYAkfy2S",
|
|
|
|
type: "message",
|
|
|
|
role: "assistant",
|
|
|
|
model: "claude-3-haiku-20240307",
|
|
|
|
content: [
|
|
|
|
{ type: "text", text: "Here is the calculation:" },
|
|
|
|
{
|
|
|
|
type: "tool_use",
|
|
|
|
id: "toolu_012kBdhG4eHaV68W56p4N94h",
|
|
|
|
name: "calculate",
|
|
|
|
input: {
|
|
|
|
expression: "2758975 + 21.11",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
stop_reason: "tool_use",
|
|
|
|
stop_sequence: nil,
|
|
|
|
usage: {
|
|
|
|
input_tokens: 345,
|
|
|
|
output_tokens: 65,
|
|
|
|
},
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
stub_request(:post, "https://api.anthropic.com/v1/messages").to_return(body: body)
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
result = proxy.generate(prompt, user: Discourse.system_user)
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
expected = <<~TEXT.strip
|
2024-03-18 15:48:46 -04:00
|
|
|
<function_calls>
|
|
|
|
<invoke>
|
2024-06-05 18:34:23 -04:00
|
|
|
<tool_name>calculate</tool_name>
|
|
|
|
<parameters><expression>2758975 + 21.11</expression></parameters>
|
|
|
|
<tool_id>toolu_012kBdhG4eHaV68W56p4N94h</tool_id>
|
2024-03-18 15:48:46 -04:00
|
|
|
</invoke>
|
|
|
|
</function_calls>
|
2024-06-05 18:34:23 -04:00
|
|
|
TEXT
|
2024-03-18 15:48:46 -04:00
|
|
|
|
|
|
|
expect(result.strip).to eq(expected)
|
|
|
|
end
|
|
|
|
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
it "can send images via a completion prompt" do
|
|
|
|
prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
"You are image bot",
|
|
|
|
messages: [type: :user, id: "user1", content: "hello", upload_ids: [upload100x100.id]],
|
|
|
|
)
|
|
|
|
|
|
|
|
encoded = prompt.encoded_uploads(prompt.messages.last)
|
|
|
|
|
|
|
|
request_body = {
|
|
|
|
model: "claude-3-opus-20240229",
|
|
|
|
max_tokens: 3000,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
role: "user",
|
|
|
|
content: [
|
|
|
|
{
|
|
|
|
type: "image",
|
|
|
|
source: {
|
|
|
|
type: "base64",
|
|
|
|
media_type: "image/jpeg",
|
|
|
|
data: encoded[0][:base64],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ type: "text", text: "user1: hello" },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
system: "You are image bot",
|
|
|
|
}
|
|
|
|
|
|
|
|
response_body = <<~STRING
|
|
|
|
{
|
|
|
|
"content": [
|
|
|
|
{
|
|
|
|
"text": "What a cool image",
|
|
|
|
"type": "text"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
|
|
|
|
"model": "claude-3-opus-20240229",
|
|
|
|
"role": "assistant",
|
|
|
|
"stop_reason": "end_turn",
|
|
|
|
"stop_sequence": null,
|
|
|
|
"type": "message",
|
|
|
|
"usage": {
|
|
|
|
"input_tokens": 10,
|
|
|
|
"output_tokens": 25
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STRING
|
|
|
|
|
|
|
|
requested_body = nil
|
|
|
|
stub_request(:post, "https://api.anthropic.com/v1/messages").with(
|
|
|
|
body:
|
|
|
|
proc do |req_body|
|
|
|
|
requested_body = JSON.parse(req_body, symbolize_names: true)
|
|
|
|
true
|
|
|
|
end,
|
|
|
|
).to_return(status: 200, body: response_body)
|
|
|
|
|
|
|
|
result = llm.generate(prompt, user: Discourse.system_user)
|
|
|
|
|
|
|
|
expect(result).to eq("What a cool image")
|
|
|
|
expect(requested_body).to eq(request_body)
|
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
it "can operate in regular mode" do
|
|
|
|
body = <<~STRING
|
|
|
|
{
|
|
|
|
"content": [
|
|
|
|
{
|
|
|
|
"text": "Hello!",
|
|
|
|
"type": "text"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
|
|
|
|
"model": "claude-3-opus-20240229",
|
|
|
|
"role": "assistant",
|
|
|
|
"stop_reason": "end_turn",
|
|
|
|
"stop_sequence": null,
|
|
|
|
"type": "message",
|
|
|
|
"usage": {
|
|
|
|
"input_tokens": 10,
|
|
|
|
"output_tokens": 25
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STRING
|
|
|
|
|
|
|
|
parsed_body = nil
|
|
|
|
stub_request(:post, "https://api.anthropic.com/v1/messages").with(
|
|
|
|
body:
|
|
|
|
proc do |req_body|
|
|
|
|
parsed_body = JSON.parse(req_body, symbolize_names: true)
|
|
|
|
true
|
|
|
|
end,
|
|
|
|
headers: {
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"X-Api-Key" => "123",
|
|
|
|
"Anthropic-Version" => "2023-06-01",
|
|
|
|
},
|
|
|
|
).to_return(status: 200, body: body)
|
|
|
|
|
|
|
|
result = llm.generate(prompt, user: Discourse.system_user)
|
|
|
|
expect(result).to eq("Hello!")
|
|
|
|
|
|
|
|
expected_body = {
|
|
|
|
model: "claude-3-opus-20240229",
|
|
|
|
max_tokens: 3000,
|
|
|
|
messages: [{ role: "user", content: "user1: hello" }],
|
|
|
|
system: "You are hello bot",
|
|
|
|
}
|
|
|
|
expect(parsed_body).to eq(expected_body)
|
|
|
|
|
|
|
|
log = AiApiAuditLog.order(:id).last
|
|
|
|
expect(log.provider_id).to eq(AiApiAuditLog::Provider::Anthropic)
|
|
|
|
expect(log.request_tokens).to eq(10)
|
|
|
|
expect(log.response_tokens).to eq(25)
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|