From 59c45d22811e0f35ad7407f73f6dbd8a6153abea Mon Sep 17 00:00:00 2001 From: Alexander Myasoedov Date: Thu, 19 Dec 2024 21:47:23 +0200 Subject: [PATCH] feat(add new spec samples): --- agentic_security/http_spec.py | 21 +++++++++++++-- agentic_security/routes/scan.py | 2 +- agentic_security/static/base.js | 31 ++++++++++++++++++++++ agentic_security/static/index.html | 20 +++++++++----- agentic_security/static/main.js | 3 +++ agentic_security/static/partials/head.html | 10 +++++++ 6 files changed, 77 insertions(+), 10 deletions(-) diff --git a/agentic_security/http_spec.py b/agentic_security/http_spec.py index 9471a52..80165f9 100644 --- a/agentic_security/http_spec.py +++ b/agentic_security/http_spec.py @@ -1,9 +1,17 @@ +import base64 + import httpx from pydantic import BaseModel -class InvalidHTTPSpecError(Exception): - ... +def encode_image_base64_by_url(url: str = "https://github.com/fluidicon.png") -> str: + """Encode image data to base64 from a URL""" + response = httpx.get(url) + encoded_content = base64.b64encode(response.content).decode("utf-8") + return "data:image/jpeg;base64," + encoded_content + + +class InvalidHTTPSpecError(Exception): ... class LLMSpec(BaseModel): @@ -72,6 +80,15 @@ class LLMSpec(BaseModel): return response + async def verify(self) -> httpx.Response: + match self: + case LLMSpec(has_image=True): + return await self.probe("test", encode_image_base64_by_url()) + case LLMSpec(has_files=True): + return await self._probe_with_files({}) + case _: + return await self.probe("test prompt") + fn = probe diff --git a/agentic_security/routes/scan.py b/agentic_security/routes/scan.py index 4c04455..a7fbb57 100644 --- a/agentic_security/routes/scan.py +++ b/agentic_security/routes/scan.py @@ -14,7 +14,7 @@ router = APIRouter() @router.post("/verify") async def verify(info: LLMInfo): spec = LLMSpec.from_string(info.spec) - r = await spec.probe("test") + r = await spec.verify() if r.status_code >= 400: raise HTTPException(status_code=r.status_code, detail=r.text) return dict( diff --git a/agentic_security/static/base.js b/agentic_security/static/base.js index d36ad7a..d0d17e5 100644 --- a/agentic_security/static/base.js +++ b/agentic_security/static/base.js @@ -64,5 +64,36 @@ Content-Type: application/json {"role": "user", "content": "<>"} ] } +`, + `POST ${URL}/v1/self-probe-image +Authorization: Bearer XXXXX +Content-Type: application/json + +[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What is in this image?", + }, + { + "type": "image_url", + "image_url": { + "url": f"data:image/jpeg;base64,{<>}" + }, + }, + ], + } +] +`, + `POST ${URL}/v1/self-probe-file +Authorization: Bearer $GROQ_API_KEY +Content-Type: multipart/form-data + +{ + "file": "@./sample_audio.m4a", + "model": "whisper-large-v3" +} `, ] diff --git a/agentic_security/static/index.html b/agentic_security/static/index.html index 3ab7dd7..04f9dcf 100644 --- a/agentic_security/static/index.html +++ b/agentic_security/static/index.html @@ -34,17 +34,23 @@ [[% include "partials/concent.html" %]]
-

Select a Config

-
-
+
+ class="flex-none w-1/2 sm:w-1/3 md:w-1/4 lg:w-1/5 border-2 rounded-lg p-4 flex flex-col items-start transition-all hover:shadow-md cursor-pointer" + :class="{ + 'border-dark-accent-green': selectedConfig === index, + 'border-gray-600': selectedConfig !== index + }">
{{ config.name }}
-
{{config.customInstructions || - 'Requires API key'}}
+
+ {{ config.customInstructions || 'Requires API key' }} +
API
diff --git a/agentic_security/static/main.js b/agentic_security/static/main.js index 6545b1b..273eb5d 100644 --- a/agentic_security/static/main.js +++ b/agentic_security/static/main.js @@ -35,6 +35,9 @@ var app = new Vue({ { name: 'Replicate', prompts: 40000 }, { name: 'Groq', prompts: 40000 }, { name: 'Together.ai', prompts: 40000 }, + { name: 'Custom API Image', prompts: 40000, customInstructions: 'Requires api spec' }, + { name: 'Custom API Files', prompts: 40000, customInstructions: 'Requires api spec' }, + ], dataConfig: [], }, diff --git a/agentic_security/static/partials/head.html b/agentic_security/static/partials/head.html index e1ffc9b..104f215 100644 --- a/agentic_security/static/partials/head.html +++ b/agentic_security/static/partials/head.html @@ -38,4 +38,14 @@ } } +