initial client + server for agentic system

This commit is contained in:
Ashwin Bharambe 2024-07-09 22:17:36 -07:00
parent 13e1667e7a
commit beb2870750
8 changed files with 166 additions and 3 deletions

View file

@ -1,2 +1,4 @@
json-strong-typing json-strong-typing
python-openapi python-openapi
flask
requests

View file

@ -196,6 +196,12 @@ class AgenticSystem(Protocol):
AgenticSystemExecuteResponse, AgenticSystemExecuteResponseStreamChunk AgenticSystemExecuteResponse, AgenticSystemExecuteResponseStreamChunk
]: ... ]: ...
@webmethod(route="/agentic_system/delete")
def delete_agentic_system(
self,
agent_id: str,
) -> None: ...
class LlamaStackEndpoints(Inference, AgenticSystem): ... class LlamaStackEndpoints(Inference, AgenticSystem): ...

59
source/client.py Normal file
View file

@ -0,0 +1,59 @@
import requests
from dataclasses import dataclass, field, asdict
from typing import List, Set, Optional, Union, Protocol
from enum import Enum
import json
from model_types import *
from agentic_system_types import *
from api_definitions import *
class EnumEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Enum):
return obj.value
elif isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
class AgenticSystemClient:
def __init__(self, base_url: str):
self.base_url = base_url
def create_agentic_system(self, request: AgenticSystemCreateRequest) -> AgenticSystemCreateResponse:
response = requests.post(f"{self.base_url}/agentic_system/create", data=json.dumps(asdict(request), cls=EnumEncoder), headers={'Content-Type': 'application/json'})
response.raise_for_status()
return AgenticSystemCreateResponse(**response.json())
def execute_agentic_system(self, request: AgenticSystemExecuteRequest) -> Union[AgenticSystemExecuteResponse, AgenticSystemExecuteResponseStreamChunk]:
response = requests.post(f"{self.base_url}/agentic_system/execute", data=json.dumps(asdict(request), cls=EnumEncoder), headers={'Content-Type': 'application/json'})
response.raise_for_status()
response_json = response.json()
if 'turn' in response_json:
return AgenticSystemExecuteResponse(**response_json)
else:
return AgenticSystemExecuteResponseStreamChunk(**response_json)
# Example usage
if __name__ == "__main__":
client = AgenticSystemClient("http://localhost:5000")
# Create a new agentic system
create_request = AgenticSystemCreateRequest(
instructions="Your instructions here",
model=InstructModel.llama3_8b_chat,
)
create_response = client.create_agentic_system(create_request)
print("Agent ID:", create_response.agent_id)
# Execute the agentic system
execute_request = AgenticSystemExecuteRequest(
agent_id=create_response.agent_id,
messages=[Message(role="user", content="Tell me a joke")],
turn_history=[],
stream=False
)
execute_response = client.execute_agentic_system(execute_request)
print("Execute Response:", execute_response)

Binary file not shown.

14
source/create_code.sh Normal file
View file

@ -0,0 +1,14 @@
#!/bin/bash
set -euo pipefail
set -x
export JAVA_HOME=/usr/local/java-runtime/impl/11
$JAVA_HOME/bin/java -jar codegen/openapi-generator-cli.jar \
generate \
-i openapi.yaml \
-g python-flask \
-o /tmp/foo \
--log-to-stderr \
--global-property debugModels,debugOperations,debugOpenAPI,debugSupportingFiles

View file

@ -96,6 +96,28 @@
} }
} }
}, },
"/agentic_system/delete": {
"delete": {
"responses": {
"200": {
"description": "OK"
}
},
"tags": [
"AgenticSystem"
],
"parameters": [
{
"name": "agent_id",
"in": "query",
"required": true,
"schema": {
"type": "string"
}
}
]
}
},
"/chat_completion": { "/chat_completion": {
"post": { "post": {
"responses": { "responses": {
@ -1440,10 +1462,10 @@
], ],
"tags": [ "tags": [
{ {
"name": "Inference" "name": "AgenticSystem"
}, },
{ {
"name": "AgenticSystem" "name": "Inference"
}, },
{ {
"name": "ShieldConfig", "name": "ShieldConfig",

View file

@ -803,6 +803,19 @@ paths:
description: OK description: OK
tags: tags:
- AgenticSystem - AgenticSystem
/agentic_system/delete:
delete:
parameters:
- in: query
name: agent_id
required: true
schema:
type: string
responses:
'200':
description: OK
tags:
- AgenticSystem
/agentic_system/execute: /agentic_system/execute:
post: post:
parameters: [] parameters: []
@ -870,8 +883,8 @@ security:
servers: servers:
- url: http://llama.meta.com - url: http://llama.meta.com
tags: tags:
- name: Inference
- name: AgenticSystem - name: AgenticSystem
- name: Inference
- description: <SchemaDefinition schemaRef="#/components/schemas/ShieldConfig" /> - description: <SchemaDefinition schemaRef="#/components/schemas/ShieldConfig" />
name: ShieldConfig name: ShieldConfig
- description: <SchemaDefinition schemaRef="#/components/schemas/AgenticSystemCreateRequest" - description: <SchemaDefinition schemaRef="#/components/schemas/AgenticSystemCreateRequest"

47
source/server.py Normal file
View file

@ -0,0 +1,47 @@
from flask import Flask, request, jsonify
from dataclasses import dataclass, field
from typing import List, Set, Optional, Union, Protocol
from enum import Enum
app = Flask(__name__)
from model_types import *
from agentic_system_types import *
from api_definitions import *
class AgenticSystemImpl(AgenticSystem):
def create_agentic_system(self, request: AgenticSystemCreateRequest) -> AgenticSystemCreateResponse:
# Mock implementation
return AgenticSystemCreateResponse(agent_id="12345")
def create_agentic_system_execute(self, request: AgenticSystemExecuteRequest) -> Union[AgenticSystemExecuteResponse, AgenticSystemExecuteResponseStreamChunk]:
# Mock implementation
return AgenticSystemExecuteResponse(
turn=AgenticSystemTurn(
user_messages=[],
steps=[],
response_message=Message(
role="assistant",
content="Hello, I am an agent. I can help you with your tasks. What can I help you with?",
)
)
)
agentic_system = AgenticSystemImpl()
@app.route("/agentic_system/create", methods=["POST"])
def create_agentic_system():
data = request.json
create_request = AgenticSystemCreateRequest(**data)
response = agentic_system.create_agentic_system(create_request)
return jsonify(response)
@app.route("/agentic_system/execute", methods=["POST"])
def create_agentic_system_execute():
data = request.json
execute_request = AgenticSystemExecuteRequest(**data)
response = agentic_system.create_agentic_system_execute(execute_request)
return jsonify(response)
if __name__ == "__main__":
app.run(debug=True)