Class: Langchain::LLM::GoogleVertexAI
- Defined in:
- lib/langchain/llm/google_vertex_ai.rb
Overview
Wrapper around the Google Vertex AI APIs: cloud.google.com/vertex-ai
Gem requirements:
gem "googleauth"
Usage:
llm = Langchain::LLM::GoogleVertexAI.new(project_id: ENV["GOOGLE_VERTEX_AI_PROJECT_ID"], region: "us-central1")
Constant Summary collapse
- DEFAULTS =
{ temperature: 0.1, max_output_tokens: 1000, top_p: 0.8, top_k: 40, dimensions: 768, embedding_model: "textembedding-gecko", chat_model: "gemini-1.0-pro" }.freeze
Instance Attribute Summary collapse
-
#authorizer ⇒ Object
readonly
Google Cloud has a project id and a specific region of deployment.
-
#defaults ⇒ Object
readonly
Google Cloud has a project id and a specific region of deployment.
-
#url ⇒ Object
readonly
Google Cloud has a project id and a specific region of deployment.
Attributes inherited from Base
Instance Method Summary collapse
-
#chat(params = {}) ⇒ Langchain::LLM::GoogleGeminiResponse
Generate a chat completion for given messages.
-
#embed(text:, model: ) ⇒ Langchain::LLM::GoogleGeminiResponse
Generate an embedding for a given text.
-
#initialize(project_id:, region:, default_options: {}) ⇒ GoogleVertexAI
constructor
A new instance of GoogleVertexAI.
Methods inherited from Base
#chat_parameters, #complete, #default_dimension, #default_dimensions, #summarize
Methods included from DependencyHelper
Constructor Details
#initialize(project_id:, region:, default_options: {}) ⇒ GoogleVertexAI
Returns a new instance of GoogleVertexAI.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/langchain/llm/google_vertex_ai.rb', line 28 def initialize(project_id:, region:, default_options: {}) depends_on "googleauth" @authorizer = ::Google::Auth.get_application_default(scope: [ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/generative-language.retriever" ]) proj_id = project_id || @authorizer.project_id || @authorizer.quota_project_id @url = "https://#{region}-aiplatform.googleapis.com/v1/projects/#{proj_id}/locations/#{region}/publishers/google/models/" @defaults = DEFAULTS.merge() chat_parameters.update( model: {default: @defaults[:chat_model]}, temperature: {default: @defaults[:temperature]}, safety_settings: {default: @defaults[:safety_settings]} ) chat_parameters.remap( messages: :contents, system: :system_instruction, tool_choice: :tool_config ) end |
Instance Attribute Details
#authorizer ⇒ Object (readonly)
Google Cloud has a project id and a specific region of deployment. For GenAI-related things, a safe choice is us-central1.
26 27 28 |
# File 'lib/langchain/llm/google_vertex_ai.rb', line 26 def @authorizer end |
#defaults ⇒ Object (readonly)
Google Cloud has a project id and a specific region of deployment. For GenAI-related things, a safe choice is us-central1.
26 27 28 |
# File 'lib/langchain/llm/google_vertex_ai.rb', line 26 def defaults @defaults end |
#url ⇒ Object (readonly)
Google Cloud has a project id and a specific region of deployment. For GenAI-related things, a safe choice is us-central1.
26 27 28 |
# File 'lib/langchain/llm/google_vertex_ai.rb', line 26 def url @url end |
Instance Method Details
#chat(params = {}) ⇒ Langchain::LLM::GoogleGeminiResponse
Generate a chat completion for given messages
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/langchain/llm/google_vertex_ai.rb', line 80 def chat(params = {}) params[:system] = {parts: [{text: params[:system]}]} if params[:system] params[:tools] = {function_declarations: params[:tools]} if params[:tools] raise ArgumentError.new("messages argument is required") if Array(params[:messages]).empty? parameters = chat_parameters.to_params(params) parameters[:generation_config] = {temperature: parameters.delete(:temperature)} if parameters[:temperature] uri = URI("#{url}#{parameters[:model]}:generateContent") parsed_response = http_post(uri, parameters) wrapped_response = Langchain::LLM::GoogleGeminiResponse.new(parsed_response, model: parameters[:model]) if wrapped_response.chat_completion || Array(wrapped_response.tool_calls).any? wrapped_response else raise StandardError.new(parsed_response) end end |
#embed(text:, model: ) ⇒ Langchain::LLM::GoogleGeminiResponse
Generate an embedding for a given text
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/langchain/llm/google_vertex_ai.rb', line 59 def ( text:, model: @defaults[:embedding_model] ) params = {instances: [{content: text}]} uri = URI("#{url}#{model}:predict") parsed_response = http_post(uri, params) Langchain::LLM::GoogleGeminiResponse.new(parsed_response, model: model) end |