Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/finic-ai/finic
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwcfan committed Sep 11, 2024
2 parents b7bf9a9 + 2f58859 commit 120b7e7
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion python_library/finicapi.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: finicapi
Version: 0.1.7
Version: 0.1.8
Summary: Finic.ai is a platform for deploying integrations and workflow automations in Python. This is the Python client for Finic
Author: Ayan Bandyopadhyay
Author-email: [email protected]
Expand Down
6 changes: 5 additions & 1 deletion python_library/finicapi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def deploy(argv=sys.argv):
print("Please specify the agent_id in the finic_config.json file")
return
agent_id = config["agent_id"]
if "num_retries" not in config:
print("Please specify the num_retries in the finic_config.json file")
return
num_retries = config["num_retries"]

finic = Finic(api_key=api_key, url=server_url)

Expand All @@ -57,6 +61,6 @@ def deploy(argv=sys.argv):
f"git ls-files -z --others --exclude-standard | xargs -0 zip -r {zip_file} && git ls-files -z | xargs -0 zip -ur {zip_file}"
)

result = finic.deploy_agent(agent_id, agent_name, zip_file)
result = finic.deploy_agent(agent_id, agent_name, num_retries, zip_file)

print(result)
31 changes: 26 additions & 5 deletions python_library/finicapi/finic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Optional
from typing import Dict, Optional, List
from functools import wraps
import json
import os
Expand Down Expand Up @@ -28,7 +28,9 @@ def __init__(
else:
self.url = "https://finic-521298051240.us-central1.run.app"

def deploy_agent(self, agent_id: str, agent_name: str, project_zipfile: str):
def deploy_agent(
self, agent_id: str, agent_name: str, num_retries: int, project_zipfile: str
):
with open(project_zipfile, "rb") as f:
upload_file = f.read()

Expand All @@ -41,6 +43,7 @@ def deploy_agent(self, agent_id: str, agent_name: str, project_zipfile: str):
json={
"agent_id": agent_id,
"agent_name": agent_name,
"num_retries": num_retries,
},
)

Expand All @@ -62,6 +65,7 @@ def deploy_agent(self, agent_id: str, agent_name: str, project_zipfile: str):
json={
"agent_id": agent_id,
"agent_name": agent_name,
"num_retries": num_retries,
},
)

Expand All @@ -84,9 +88,26 @@ def get_run_status(self, agent_id: str, run_id: str):
# TODO
pass

def log_run_status(self, agent_id: str, status: str, message: str):
# TODO
print(f"Logging status: {status} - {message}")
def log_attempt(self, agent_id: str, success: bool, logs: List[str], result: Dict):
if self.environment == FinicEnvironment.LOCAL:
if success:
print("Successful execution. Results: ", result)
else:
print("Failed execution. Logs: ", logs)
else:
requests.post(
f"{self.url}/log-execution-attempt",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
json={
"agent_id": agent_id,
"success": success,
"logs": logs,
"result": result,
},
)

def workflow_entrypoint(self, func):

Expand Down
2 changes: 1 addition & 1 deletion python_library/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="finicapi",
version="0.1.7",
version="0.1.8",
description="Finic.ai is a platform for deploying integrations and workflow automations in Python. This is the Python client for Finic",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
8 changes: 5 additions & 3 deletions server/agent_deployer/agent_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def __init__(self, db: Database, config: AppConfig):
self.build_client = cloudbuild_v1.CloudBuildClient(credentials=credentials)
self.jobs_client = run_v2.JobsClient(credentials=credentials)

async def get_agent_upload_link(self, agent: Agent, expiration_minutes: int = 15) -> str:
async def get_agent_upload_link(
self, agent: Agent, expiration_minutes: int = 15
) -> str:

bucket = self.storage_client.get_bucket(self.deployments_bucket)
blob = bucket.blob(f"{agent.id}.zip")
Expand All @@ -43,7 +45,7 @@ async def deploy_agent(self, agent: Agent):
# Check if the job already exists in Cloud Run
try:
self.jobs_client.get_job(
name=f"projects/{self.project_id}/locations/us-central1/jobs/job-{agent.id}"
name=f"projects/{self.project_id}/locations/us-central1/jobs/{Agent.get_cloud_job_id(agent)}"
)
job_exists = True
except Exception:
Expand Down Expand Up @@ -106,7 +108,7 @@ def _get_build_config(self, job: Agent, job_exists: bool) -> dict:
"args": [
"-c",
f"gcloud run jobs {job_command} {Agent.get_cloud_job_id(job)} --image {image_name} --region us-central1 "
f"--tasks=1 --max-retries=3 --task-timeout=86400s --memory=4Gi",
f"--tasks=1 --max-retries={job.num_retries} --task-timeout=86400s --memory=4Gi",
],
},
],
Expand Down
1 change: 1 addition & 0 deletions server/agent_runner/agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def start_agent(self, agent: Agent, input: Dict) -> Execution:
{"name": "FINIC_ENV", "value": FinicEnvironment.PROD.value},
{"name": "FINIC_INPUT", "value": json.dumps(input)},
{"name": "FINIC_API_KEY", "value": secret_key},
{"name": "FINIC_AGENT_ID", "value": agent.id},
]
}
]
Expand Down
1 change: 1 addition & 0 deletions server/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class GetExecutionRequest(BaseModel):
class DeployAgentRequest(BaseModel):
agent_id: str
agent_name: str
num_retries: int


class RunAgentRequest(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion server/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Agent(BaseModel):
name: str
status: AgentStatus
created_at: Optional[datetime.datetime] = None
url: Optional[str] = None
num_retries: int = 3

@staticmethod
def get_cloud_job_id(agent: "Agent") -> str:
Expand Down
2 changes: 1 addition & 1 deletion server/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ async def deploy_agent(
config: AppConfig = Depends(validate_token),
):
try:

agent = await db.get_agent(config=config, user_defined_id=request.agent_id)
agent.status = AgentStatus.deploying
await db.upsert_agent(agent)
Expand Down Expand Up @@ -138,6 +137,7 @@ async def get_agent_upload_link(
app_id=config.app_id,
user_defined_id=request.agent_id,
name=request.agent_name,
num_retries=request.num_retries,
status="deploying",
)
await db.upsert_agent(agent)
Expand Down

0 comments on commit 120b7e7

Please sign in to comment.