Skip to content

implement pdf for google ai #282

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

Open
wants to merge 3 commits 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
20 changes: 20 additions & 0 deletions lib/chat_models/chat_google_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,26 @@ defmodule LangChain.ChatModels.ChatGoogleAI do
}
end

def for_api(%ContentPart{type: :file, options: opts} = part) do
media = Keyword.get(opts || [], :media, nil)

mime_type =
if is_nil(media) do
message = "Received no media type for ContentPart"
Logger.error(message)
raise LangChainError, message
else
"#{media}"
end

%{
"inline_data" => %{
"mime_type" => mime_type,
"data" => part.content
}
}
end

def for_api(%ToolCall{} = call) do
%{
"functionCall" => %{
Expand Down
33 changes: 33 additions & 0 deletions test/chat_models/chat_google_ai_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,39 @@
} = msg1
end

test "turns a file ContentPart into the expected JSON format" do

Check failure on line 199 in test/chat_models/chat_google_ai_test.exs

View workflow job for this annotation

GitHub Actions / test (1.18, 27, lint)

test for_api/3 turns a file ContentPart into the expected JSON format (ChatModels.ChatGoogleAITest)

Check failure on line 199 in test/chat_models/chat_google_ai_test.exs

View workflow job for this annotation

GitHub Actions / test (1.16, 25)

test for_api/3 turns a file ContentPart into the expected JSON format (ChatModels.ChatGoogleAITest)
file_base64_data = "some_file_base64_data"
mime_type = "application/pdf"

expected = %{
"inline_data" => %{
"mime_type" => mime_type,
"data" => file_base64_data
}
}

result =
ChatGoogleAI.for_api(
ChatGoogleAI.new!(),
ContentPart.file!(file_base64_data, media: "application/pdf")
)

assert result == expected
end

test "throws an exception when no media type supplied" do

Check failure on line 219 in test/chat_models/chat_google_ai_test.exs

View workflow job for this annotation

GitHub Actions / test (1.18, 27, lint)

test for_api/3 throws an exception when no media type supplied (ChatModels.ChatGoogleAITest)

Check failure on line 219 in test/chat_models/chat_google_ai_test.exs

View workflow job for this annotation

GitHub Actions / test (1.16, 25)

test for_api/3 throws an exception when no media type supplied (ChatModels.ChatGoogleAITest)
file_base64_data = "some_file_base64_data"

assert_raise(
LangChainError,
"Received no media type for ContentPart",
ChatGoogleAI.for_api(
ChatGoogleAI.new!(),
ContentPart.file!(file_base64_data)
)
)
end

test "translates a Message with function results to the expected structure" do
expected =
%{
Expand Down
Loading