removing the tools calls
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 1m10s

This commit is contained in:
Josh Knapp 2025-02-04 13:39:34 -08:00
parent 1dd3e50729
commit b5a435913e
3 changed files with 1 additions and 131 deletions

View File

@ -1,100 +0,0 @@
"""
title: Bedrock Image Description
author: Josh Knapp
version: 0.1.0
description="Provide Direct Bedrock call for image generation"
"""
import subprocess
import json
from pydantic import BaseModel, Field
# Try to import boto3, install if not present
try:
import boto3
except ImportError:
print("boto3 package not found. Attempting to install...")
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "boto3"])
import boto3
print("boto3 package installed successfully")
except subprocess.CalledProcessError as e:
print(f"Failed to install boto3 package: {str(e)}")
class Tools:
class Valves(BaseModel):
AWS_ACCESS_KEY: str = Field(
default="",
description="AWS Access Key",
)
AWS_SECRET_KEY: str = Field(
default="",
description="AWS Secret Key",
)
AWS_BEDROCK_MODEL: str = Field(
default="",
description="AWS Bedrock Model to use"
)
def __init__(self):
self.valves = self.Valves()
pass
def analyze_image(self, base64_image: str) -> str:
"""
Analyze an image using AWS Bedrock's vision model
Args:
base64_image (str): Base64 encoded image string
Returns:
str: Description of the image
"""
try:
# Initialize Bedrock runtime client
bedrock = boto3.client(
service_name="bedrock-runtime",
aws_access_key_id=self.valves.AWS_ACCESS_KEY,
aws_secret_access_key=self.valves.AWS_SECRET_KEY,
region_name="us-east-1" # or your preferred region
)
# Prepare the request body
request_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": base64_image
}
},
{
"type": "text",
"text": "Please describe this image in detail."
}
]
}
]
}
# Invoke the model
response = bedrock.invoke_model(
modelId=self.valves.AWS_BEDROCK_MODEL,
body=json.dumps(request_body)
)
# Parse and return the response
response_body = json.loads(response['body'].read())
return response_body['messages'][0]['content'][0]['text']
except Exception as e:
print(f"Error analyzing image: {str(e)}")
return f"Error analyzing image: {str(e)}"

View File

@ -1,5 +0,0 @@
For any model to use this tool, you must set the valve values, and add something to the model to let it know to use the tool.
```
You have access to a tool that allows you to get descriptions of images called "Bedrock Image Description". Any image handling should be sent through this tool.
```

View File

@ -61,35 +61,10 @@ async def get_chat_history(channel, limit=100):
messages.append(content) messages.append(content)
return "\n".join(reversed(messages)) return "\n".join(reversed(messages))
async def get_available_tools() -> List[Dict[str, Any]]:
"""Fetch available tools from OpenWebUI API."""
try:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
async with aiohttp.ClientSession() as session:
async with session.get(
f"{OPENWEBUI_API_BASE}/v1/tools/list",
headers=headers
) as response:
if response.status == 200:
tools = await response.json()
return tools
else:
print(f"Error fetching tools: {await response.text()}")
return []
except Exception as e:
print(f"Error fetching tools: {str(e)}")
return []
async def get_ai_response(context, user_message, image_urls=None): async def get_ai_response(context, user_message, image_urls=None):
# Fetch available tools
tools = await get_available_tools()
tools_json = json.dumps(tools, indent=2)
system_message = f"\"\"\"Previous conversation context:{context}\nAvailable Tools: {tools_json}\nReturn an empty string if no tools match the query." + """If a function tool matches, construct and return a JSON object in the format {"name": "functionName", "parameters": {"requiredFunctionParamKey": "requiredFunctionParamValue\"}} using the appropriate tool and its parameters. Only return the object and limit the response to the JSON object without additional text.""" system_message = f"\"\"\"Previous conversation context:{context}"""
messages = [ messages = [
{"role": "system", "content": system_message}, {"role": "system", "content": system_message},