Skip to content

Commit

Permalink
Merge pull request #776 from Mirascope/add-provider-google
Browse files Browse the repository at this point in the history
feat: Add provider Google
  • Loading branch information
willbakst authored Feb 14, 2025
2 parents 15f8232 + 98798cf commit 309edb0
Show file tree
Hide file tree
Showing 284 changed files with 8,812 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/api/core/google/call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.call

::: mirascope.core.google.call
3 changes: 3 additions & 0 deletions docs/api/core/google/call_params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.call_params

::: mirascope.core.google.call_params
3 changes: 3 additions & 0 deletions docs/api/core/google/call_response.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.call_response

::: mirascope.core.google.call_response
3 changes: 3 additions & 0 deletions docs/api/core/google/call_response_chunk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.call_response_chunk

::: mirascope.core.google.call_response_chunk
3 changes: 3 additions & 0 deletions docs/api/core/google/dynamic_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.dynamic_config

::: mirascope.core.google.dynamic_config
3 changes: 3 additions & 0 deletions docs/api/core/google/stream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.stream

::: mirascope.core.google.stream
3 changes: 3 additions & 0 deletions docs/api/core/google/tool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mirascope.core.google.tool

::: mirascope.core.google.tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from mirascope.core import BaseDynamicConfig, BaseMessageParam, google
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

def _ask_for_help(self, question: str) -> str:
"""Asks for help from an expert."""
print("[Assistant Needs Help]")
print(f"[QUESTION]: {question}")
answer = input("[ANSWER]: ")
print("[End Help]")
return answer

@google.call("gemini-1.5-flash")
def _call(self, query: str) -> BaseDynamicConfig:
messages = [
BaseMessageParam(role="system", content="You are a librarian"),
*self.history,
BaseMessageParam(role="user", content=query),
]
return {"messages": messages, "tools": [self._ask_for_help]}

def _step(self, query: str) -> str:
if query:
self.history.append(BaseMessageParam(role="user", content=query))
response = self._call(query)
self.history.append(response.message_param)
tools_and_outputs = []
if tools := response.tools:
for tool in tools:
print(f"[Calling Tool '{tool._name()}' with args {tool.args}]")
tools_and_outputs.append((tool, tool.call()))
self.history += response.tool_message_params(tools_and_outputs)
return self._step("")
else:
return response.content

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
step_output = self._step(query)
print(step_output)


Librarian().run()
50 changes: 50 additions & 0 deletions examples/learn/agents/human_in_the_loop/google/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from mirascope.core import BaseDynamicConfig, Messages, google
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

def _ask_for_help(self, question: str) -> str:
"""Asks for help from an expert."""
print("[Assistant Needs Help]")
print(f"[QUESTION]: {question}")
answer = input("[ANSWER]: ")
print("[End Help]")
return answer

@google.call("gemini-1.5-flash")
def _call(self, query: str) -> BaseDynamicConfig:
messages = [
Messages.System("You are a librarian"),
*self.history,
Messages.User(query),
]
return {"messages": messages, "tools": [self._ask_for_help]}

def _step(self, query: str) -> str:
if query:
self.history.append(Messages.User(query))
response = self._call(query)
self.history.append(response.message_param)
tools_and_outputs = []
if tools := response.tools:
for tool in tools:
print(f"[Calling Tool '{tool._name()}' with args {tool.args}]")
tools_and_outputs.append((tool, tool.call()))
self.history += response.tool_message_params(tools_and_outputs)
return self._step("")
else:
return response.content

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
step_output = self._step(query)
print(step_output)


Librarian().run()
50 changes: 50 additions & 0 deletions examples/learn/agents/human_in_the_loop/google/shorthand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from mirascope.core import BaseDynamicConfig, Messages, google
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

def _ask_for_help(self, question: str) -> str:
"""Asks for help from an expert."""
print("[Assistant Needs Help]")
print(f"[QUESTION]: {question}")
answer = input("[ANSWER]: ")
print("[End Help]")
return answer

@google.call("gemini-1.5-flash")
def _call(self, query: str) -> BaseDynamicConfig:
messages = [
Messages.System("You are a librarian"),
*self.history,
Messages.User(query),
]
return {"messages": messages, "tools": [self._ask_for_help]}

def _step(self, query: str) -> str:
if query:
self.history.append(Messages.User(query))
response = self._call(query)
self.history.append(response.message_param)
tools_and_outputs = []
if tools := response.tools:
for tool in tools:
print(f"[Calling Tool '{tool._name()}' with args {tool.args}]")
tools_and_outputs.append((tool, tool.call()))
self.history += response.tool_message_params(tools_and_outputs)
return self._step("")
else:
return response.content

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
step_output = self._step(query)
print(step_output)


Librarian().run()
52 changes: 52 additions & 0 deletions examples/learn/agents/human_in_the_loop/google/string_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from mirascope.core import BaseDynamicConfig, Messages, google, prompt_template
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

def _ask_for_help(self, question: str) -> str:
"""Asks for help from an expert."""
print("[Assistant Needs Help]")
print(f"[QUESTION]: {question}")
answer = input("[ANSWER]: ")
print("[End Help]")
return answer

@google.call("gemini-1.5-flash")
@prompt_template(
"""
SYSTEM: You are a librarian
MESSAGES: {self.history}
USER: {query}
"""
)
def _call(self, query: str) -> BaseDynamicConfig:
return {"tools": [self._ask_for_help]}

def _step(self, query: str) -> str:
if query:
self.history.append(Messages.User(query))
response = self._call(query)
self.history.append(response.message_param)
tools_and_outputs = []
if tools := response.tools:
for tool in tools:
print(f"[Calling Tool '{tool._name()}' with args {tool.args}]")
tools_and_outputs.append((tool, tool.call()))
self.history += response.tool_message_params(tools_and_outputs)
return self._step("")
else:
return response.content

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
step_output = self._step(query)
print(step_output)


Librarian().run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from mirascope.core import BaseMessageParam, google
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

@google.call("gemini-1.5-flash")
def _call(self, query: str) -> list[google.GoogleMessageParam]:
return [
BaseMessageParam(role="system", content="You are a librarian"),
*self.history,
BaseMessageParam(role="user", content=query),
]

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
response = self._call(query)
print(response.content)
self.history += [
BaseMessageParam(role="user", content=query),
response.message_param,
]


Librarian().run()
30 changes: 30 additions & 0 deletions examples/learn/agents/state_management/google/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from mirascope.core import Messages, google
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

@google.call("gemini-1.5-flash")
def _call(self, query: str) -> Messages.Type:
return [
Messages.System("You are a librarian"),
*self.history,
Messages.User(query),
]

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
response = self._call(query)
print(response.content)
self.history += [
Messages.User(query),
response.message_param,
]


Librarian().run()
30 changes: 30 additions & 0 deletions examples/learn/agents/state_management/google/shorthand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from mirascope.core import Messages, google
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

@google.call("gemini-1.5-flash")
def _call(self, query: str) -> Messages.Type:
return [
Messages.System("You are a librarian"),
*self.history,
Messages.User(query),
]

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
response = self._call(query)
print(response.content)
self.history += [
Messages.User(query),
response.message_param,
]


Librarian().run()
32 changes: 32 additions & 0 deletions examples/learn/agents/state_management/google/string_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from mirascope.core import Messages, google, prompt_template
from pydantic import BaseModel


class Librarian(BaseModel):
history: list[google.GoogleMessageParam] = []

@google.call("gemini-1.5-flash")
@prompt_template(
"""
SYSTEM: You are a librarian
MESSAGES: {self.history}
USER: {query}
"""
)
def _call(self, query: str): ...

def run(self) -> None:
while True:
query = input("(User): ")
if query in ["exit", "quit"]:
break
print("(Assistant): ", end="", flush=True)
response = self._call(query)
print(response.content)
self.history += [
Messages.User(query),
response.message_param,
]


Librarian().run()
Loading

0 comments on commit 309edb0

Please sign in to comment.