-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertex_ai.exs
More file actions
136 lines (112 loc) · 4.28 KB
/
vertex_ai.exs
File metadata and controls
136 lines (112 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env elixir
# Nous AI - Google Vertex AI Provider
#
# Vertex AI provides enterprise access to Gemini models with features like
# VPC-SC, CMEK, regional endpoints, and IAM-based access control.
#
# Prerequisites:
# - A Google Cloud project with Vertex AI API enabled
# - Authentication (one of):
# a) Access token: `export VERTEX_AI_ACCESS_TOKEN=$(gcloud auth print-access-token)`
# b) Goth with service account: `export GOOGLE_APPLICATION_CREDENTIALS=/path/to/sa.json`
# - Project configuration:
# `export GOOGLE_CLOUD_PROJECT=your-project-id`
# `export GOOGLE_CLOUD_REGION=us-central1` (optional, defaults to us-central1)
IO.puts("=== Nous AI - Vertex AI Provider ===\n")
# ============================================================================
# Option 1: Using environment variables
# ============================================================================
IO.puts("--- Setup with Environment Variables ---")
project = System.get_env("GOOGLE_CLOUD_PROJECT")
token = System.get_env("VERTEX_AI_ACCESS_TOKEN")
if project && token do
IO.puts("Project: #{project}")
IO.puts("Region: #{System.get_env("GOOGLE_CLOUD_REGION", "us-central1")}\n")
# With env vars set, just use the model string
agent =
Nous.new("vertex_ai:gemini-2.0-flash",
instructions: "You are a helpful assistant. Be concise."
)
case Nous.run(agent, "What is Elixir? Answer in one sentence.") do
{:ok, result} ->
IO.puts("Response: #{result.output}")
IO.puts("Tokens: #{result.usage.total_tokens}")
{:error, error} ->
IO.puts("Error: #{inspect(error)}")
end
else
IO.puts("""
Skipping: Set these environment variables to test:
export GOOGLE_CLOUD_PROJECT=your-project-id
export VERTEX_AI_ACCESS_TOKEN=$(gcloud auth print-access-token)
""")
end
IO.puts("")
# ============================================================================
# Option 2: Explicit configuration
# ============================================================================
IO.puts("--- Explicit Configuration ---")
IO.puts("""
# Pass base_url and api_key directly:
model = Nous.Model.parse("vertex_ai:gemini-2.0-flash",
base_url: Nous.Providers.VertexAI.endpoint("my-project", "us-central1"),
api_key: access_token
)
""")
# ============================================================================
# Option 3: Using Goth (recommended for production)
# ============================================================================
IO.puts("--- Goth Integration (Production) ---")
IO.puts("""
# 1. Add {:goth, "~> 1.4"} to your deps
# 2. Start Goth in your supervision tree:
#
# children = [
# {Goth, name: MyApp.Goth}
# ]
#
# 3. Set GOOGLE_APPLICATION_CREDENTIALS to your service account JSON
# 4. Configure Nous:
#
# config :nous, :vertex_ai,
# goth: MyApp.Goth,
# base_url: "https://us-central1-aiplatform.googleapis.com/v1/projects/my-project/locations/us-central1"
#
# 5. Use it:
# agent = Nous.new("vertex_ai:gemini-2.0-flash")
""")
# ============================================================================
# Streaming
# ============================================================================
IO.puts("--- Streaming ---")
if project && token do
agent =
Nous.new("vertex_ai:gemini-2.0-flash",
instructions: "You are a helpful assistant."
)
case Nous.run_stream(agent, "Write a haiku about Elixir.") do
{:ok, stream} ->
stream
|> Enum.each(fn
{:text_delta, text} -> IO.write(text)
{:finish, _} -> IO.puts("\n")
_ -> :ok
end)
{:error, error} ->
IO.puts("Streaming error: #{inspect(error)}")
end
else
IO.puts("Skipping streaming demo (no credentials)\n")
end
# ============================================================================
# Available Gemini Models on Vertex AI
# ============================================================================
IO.puts("--- Available Models ---")
IO.puts("""
Model | Description
-------------------------------|-------------------------------------------
gemini-2.0-flash | Fast, efficient for most tasks
gemini-2.0-flash-lite | Lightweight, lowest latency
gemini-2.5-pro-preview-06-05 | Most capable, best for complex reasoning
gemini-2.5-flash-preview-05-20 | Balanced speed and capability
""")