Skip to content

Commit

Permalink
Pre-pylint refactoring, part I, issues #3, #5 and #7
Browse files Browse the repository at this point in the history
  • Loading branch information
StashaS committed Sep 16, 2024
1 parent 6686537 commit 51cd14b
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 166 deletions.
98 changes: 63 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ This project uses Gemini via Google Cloud API. To use the project you must first

### Allow requesting LLM models via APIs
1. The project uses [Vertex AI API](https://cloud.google.com/vertex-ai/) to request Gemini models, Vertex AI API should be allowed in your Google cloud account. You need to allow it from the [Google Cloud Vertex AI API product page](https://console.cloud.google.com/apis/library/aiplatform.googleapis.com).
2. [Optional] With this [instructions](https://cloud.google.com/apis/docs/capping-api-usage) you can limit or increase your API requests quota.
2. [Optional] With these [instructions](https://cloud.google.com/apis/docs/capping-api-usage) you can limit or increase your API requests quota.

### Set up gcloud tool in your environment
Follow the Google instructions to [install](https://cloud.google.com/sdk/docs/install) gcloud and to [initialize](https://cloud.google.com/sdk/docs/initializing) it.
Expand Down Expand Up @@ -125,21 +125,21 @@ In this example, you have created a custom function and gave Gemini ability to u
<summary>See code</summary>

```python
import config.config as config
from gemini_agents_toolkit import agent

import vertexai
from config import (PROJECT_ID, REGION, SIMPLE_MODEL)
from gemini_agents_toolkit import agent


def say_to_duck(say: str):
"""say something to a duck"""
return f"duck answer is: duck duck {say} duck duck duck"


vertexai.init(project=config.project_id, location=config.region)
vertexai.init(project=PROJECT_ID, location=REGION)

all_functions = [say_to_duck]
duck_comms_agent = agent.create_agent_from_functions_list(functions=all_functions, model_name=config.simple_model)
duck_comms_agent = agent.create_agent_from_functions_list(functions=all_functions,
model_name=SIMPLE_MODEL)

print(duck_comms_agent.send_message("say to the duck message: I am hungry"))
```
Expand All @@ -156,40 +156,47 @@ python examples/multi_agent_example.py
<summary>See code</summary>

```python
import config.config as config
from gemini_agents_toolkit import agent

import datetime
import vertexai
from config import (PROJECT_ID, REGION, SIMPLE_MODEL)
from gemini_agents_toolkit import agent


vertexai.init(project=config.project_id, location=config.region)
vertexai.init(project=PROJECT_ID, location=REGION)


def generate_duck_comms_agent():
"""create an agent to say to a duck"""

def say_to_duck(say: str):
"""say something to a duck"""
return f"duck answer is: duck duck {say} duck duck duck"

return agent.create_agent_from_functions_list(
functions=[say_to_duck],
delegation_function_prompt="Agent can communicat to ducks and can say something to them. And provides the answer from the duck.",
model_name=config.simple_model)
functions=[say_to_duck],
delegation_function_prompt=("""Agent can communicat to ducks and can say something to them.
And provides the answer from the duck."""),
model_name=SIMPLE_MODEL)


def generate_time_checker_agent():
"""create an agent to get the time"""

def get_local_time():
"""get the current local time"""
import datetime
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

return agent.create_agent_from_functions_list(
functions=[get_local_time],
delegation_function_prompt="Agent can provide the current local time.",
model_name=config.simple_model)
functions=[get_local_time],
delegation_function_prompt="Agent can provide the current local time.",
model_name=SIMPLE_MODEL)


duck_comms_agent = generate_duck_comms_agent()
time_checker_agent = generate_time_checker_agent()

main_agent = agent.create_agent_from_functions_list(delegates=[time_checker_agent, duck_comms_agent], model_name=config.simple_model)
main_agent = agent.create_agent_from_functions_list(
delegates=[time_checker_agent, duck_comms_agent],
model_name=SIMPLE_MODEL)

print(main_agent.send_message("say to the duck message: I am hungry"))
print(main_agent.send_message("can you tell me what time it is?"))
Expand All @@ -208,11 +215,10 @@ python examples/simple_scheduler_example.py
<summary>See code</summary>

```python
import config.config as config
from gemini_agents_toolkit import agent
import time

import vertexai
from config import (PROJECT_ID, REGION, SIMPLE_MODEL)
from gemini_agents_toolkit import agent


def say_to_duck(say: str):
Expand All @@ -221,13 +227,17 @@ def say_to_duck(say: str):


def print_msg_from_agent(msg: str):
"""print message in console"""
print(msg)


vertexai.init(project=config.project_id, location=config.region)
vertexai.init(project=PROJECT_ID, location=REGION)

all_functions = [say_to_duck]
duck_comms_agent = agent.create_agent_from_functions_list(functions=all_functions, model_name=config.simple_model, add_scheduling_functions=True, on_message=print_msg_from_agent)
duck_comms_agent = agent.create_agent_from_functions_list(functions=all_functions,
model_name=SIMPLE_MODEL,
add_scheduling_functions=True,
on_message=print_msg_from_agent)

# no need to print result directly since we passed to agent on_message
duck_comms_agent.send_message("can you be saying, each minute, to the duck that I am hungry")
Expand All @@ -247,28 +257,46 @@ python examples/pipeline_example.py
<summary>See code</summary>

```python
import config.config as config
import vertexai
from config import (PROJECT_ID, REGION, DEFAULT_MODEL, SIMPLE_MODEL)
from gemini_agents_toolkit import agent
from gemini_agents_toolkit.pipeline.basic_step import BasicStep

import vertexai


def say_to_duck(say: str):
"""say something to a duck"""
return f"duck answer is: duck duck {say} duck duck duck"


vertexai.init(project=config.project_id, location=config.region)
vertexai.init(project=PROJECT_ID, location=REGION)

all_functions = [say_to_duck]
duck_comms_agent = agent.create_agent_from_functions_list(functions=all_functions, model_name=config.simple_model)
computation_agent = agent.create_agent_from_functions_list(model_name=config.default_model, system_instruction="you are agent design to do computation")
printing_agent = agent.create_agent_from_functions_list(model_name=config.default_model, system_instruction="you are agent design to do nice prints in the shell. You do not have to be producing BASH commands your output will be printed by other developer in the bush you are ONLY in charge of the formattin ASCII characters that will be printed. No need to even use characters like ``` before/after you response")

pipeline = (BasicStep(duck_comms_agent, "say to duck: I am hungry") |
BasicStep(computation_agent, "calculate how many times word duck was used in the response") |
BasicStep(printing_agent, "print number in a nice format, go nuts with how you want it to look"))
duck_comms_agent = agent.create_agent_from_functions_list(functions=all_functions,
model_name=SIMPLE_MODEL)
computation_agent = agent.create_agent_from_functions_list(
model_name=DEFAULT_MODEL,
system_instruction="you are agent design to do computation"
)

PRINTING_AGENT_PROMPT = """
You are agent design to do nice prints in the shell.
You do not have to be producing BASH commands,
your output will be printed by other developer in the bash.
You are ONLY in charge of the formatting ASCII characters that will be printed.
No need to even use characters like ``` before/after you response.
"""
printing_agent = agent.create_agent_from_functions_list(model_name=DEFAULT_MODEL,
system_instruction=PRINTING_AGENT_PROMPT)

pipeline = (BasicStep(duck_comms_agent, "say to duck: I am hungry") |
BasicStep(
computation_agent,
"calculate how many times word duck was used in the response"
) |
BasicStep(
printing_agent,
"print number in a nice format, go nuts with how you want it to look"
))
print(pipeline.execute())
```
</details>
Expand Down
12 changes: 12 additions & 0 deletions config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""The module to provide configuration values."""

import os

# ***** Google Cloud project settings *****
PROJECT_ID = os.getenv('GOOGLE_PROJECT')
API_KEY = os.getenv('GOOGLE_API_KEY')
REGION = os.environ.get('GOOGLE_REGION', 'us-west1')

DEFAULT_MODEL = 'gemini-1.5-pro'
# simple model is used for demo examples as a cost optimization
SIMPLE_MODEL = 'gemini-1.5-flash'
10 changes: 0 additions & 10 deletions config/config.py

This file was deleted.

60 changes: 41 additions & 19 deletions examples/investing_pipeline_example.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import config.config as config
from gemini_agents_toolkit import agent
from gemini_agents_toolkit.pipeline.eager_pipeline import EagerPipeline
"""This example illustrates creating an advanced pipeline with different step types."""

import vertexai
import random
import vertexai
from config import (PROJECT_ID, REGION, DEFAULT_MODEL)
from gemini_agents_toolkit import agent
from gemini_agents_toolkit.pipeline.eager_pipeline import EagerPipeline


# pylint: disable-next=invalid-name
current_limit_buy_price = None
# pylint: disable-next=invalid-name
current_limit_sell_price = None


def check_if_limit_sell_order_exists():
"""Check if limit sell order exists"""
global current_limit_sell_price
return current_limit_sell_price is not None


def cancel_limit_sell_order():
"""Cancel limit sell order"""
# pylint: disable-next=global-statement
global current_limit_sell_price
current_limit_sell_price = None
return "Limit sell order canceled"


def set_limit_sell_order(price: float):
"""Set limit sell order. Price is float and it can not be a formula or logic that prints formula, input HAS to be fully computed price.
"""Set limit sell order.
Price is float, and it can not be a formula or logic that prints formula,
input HAS to be fully computed price.
Args:
price: Price to set limit sell order"""
# pylint: disable-next=global-statement
global current_limit_sell_price
current_limit_sell_price = price
return f"Limit sell order set at {price}"
Expand All @@ -37,32 +42,34 @@ def check_current_tqqq_price():
"""Current price of TQQQ"""
# return 125.3 + random number in the range [-100, 100]
return 125.3 + random.randint(-100, 100)


def get_current_limit_buy_price():
"""Current limit buy price of TQQQ"""
global current_limit_buy_price
return current_limit_buy_price


def check_if_limit_buy_order_exists():
"""Check if limit buy order exists"""
global current_limit_buy_price
return current_limit_buy_price is not None


def cancel_limit_buy_order():
"""Cancel limit buy order"""
# pylint: disable-next=global-statement
global current_limit_buy_price
current_limit_buy_price = None
return "Limit buy order canceled"


def set_limit_buy_order(price: float):
"""Set limit buy order. Price is float and it can not be a formula or logic that prints formula, input HAS to be fully computed price.
"""Set limit buy order.
Price is float, and it can not be a formula or logic that prints formula,
input HAS to be fully computed price.
Args:
price: Price to set limit buy order"""
# pylint: disable-next=global-statement
global current_limit_buy_price
current_limit_buy_price = price
return f"Limit buy order set at {price}"
Expand All @@ -73,10 +80,21 @@ def check_how_many_shares_i_own():
return 30 + random.randint(-20, 1)


vertexai.init(project=config.project_id, location=config.region)
vertexai.init(project=PROJECT_ID, location=REGION)

all_functions = [check_if_limit_sell_order_exists, cancel_limit_sell_order, set_limit_sell_order, check_current_tqqq_price, check_if_limit_buy_order_exists, get_current_limit_buy_price, cancel_limit_buy_order, set_limit_buy_order, check_how_many_shares_i_own]
investor_agent = agent.create_agent_from_functions_list(functions=all_functions, model_name=config.default_model)
all_functions = [
check_if_limit_sell_order_exists,
cancel_limit_sell_order,
set_limit_sell_order,
check_current_tqqq_price,
check_if_limit_buy_order_exists,
get_current_limit_buy_price,
cancel_limit_buy_order,
set_limit_buy_order,
check_how_many_shares_i_own
]
investor_agent = agent.create_agent_from_functions_list(functions=all_functions,
model_name=DEFAULT_MODEL)

pipeline = EagerPipeline(default_agent=investor_agent)
if not pipeline.boolean_step("check if I own more than 30 shares of TQQQ"):
Expand All @@ -86,11 +104,15 @@ def check_how_many_shares_i_own():
else:
print("everywhere")
if pipeline.boolean_step("is there a limit buy exists already?"):
if not pipeline.boolean_step("is there current limit buy price lower than curent price of TQQQ -5%?"):
if not pipeline.boolean_step(
"is there current limit buy price lower than current price of TQQQ -5%?"):
pipeline.step("cancel limit buy order")
pipeline.step("""set limit buy order for TQQQ for price 3 precent below the current price.
Do not return compute formula, do compute of the price yourself in your head""")
pipeline.step(
"""set limit buy order for TQQQ for price 3 percent below the current price.
Do not return compute formula,
do compute of the price yourself in your head""")
else:
pipeline.step("""set limit buy order for TQQQ for price 3 precent below the current price.
Do not return compute formula, do compute of the price yourself in your head""")
pipeline.step(
"""set limit buy order for TQQQ for price 3 percent below the current price.
Do not return compute formula, do compute of the price yourself in your head""")
print(pipeline.summary())
Loading

0 comments on commit 51cd14b

Please sign in to comment.