feat: 添加HOST_IP环境变量支持并更新图片生成工具
- 在docker-compose中添加HOST_IP环境变量配置 - 修改minio_utils以支持通过HOST_IP配置公开端点 - 移除本地MCP服务器配置 - 将text_to_img工具替换为基于Kolors API的实现
This commit is contained in:
parent
45427b559a
commit
2acbd4b93a
@ -24,6 +24,7 @@ services:
|
||||
env_file:
|
||||
- src/.env
|
||||
environment:
|
||||
- HOST_IP=${HOST_IP:-}
|
||||
- NEO4J_URI=${NEO4J_URI:-bolt://graph:7687}
|
||||
- NEO4J_USERNAME=${NEO4J_USERNAME:-neo4j}
|
||||
- NEO4J_PASSWORD=${NEO4J_PASSWORD:-0123456789}
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
from io import BytesIO
|
||||
import os
|
||||
import requests
|
||||
|
||||
from typing import Any
|
||||
|
||||
from langchain_core.tools import tool
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from src.agents.common.tools import get_buildin_tools
|
||||
from src.utils import logger
|
||||
@ -29,35 +30,42 @@ def calculator(a: float, b: float, operation: str) -> float:
|
||||
logger.error(f"Calculator error: {e}")
|
||||
raise
|
||||
|
||||
|
||||
@tool
|
||||
async def text_to_img(text: str) -> str:
|
||||
"""
|
||||
文生图函数,根据文本生成一张包含该文本的图片,并将其上传到文件服务器,最终返回图片的公开访问 URL。
|
||||
A text-to-image function that generates an image containing the given text,
|
||||
uploads it to a file server, and returns the public URL of the image.
|
||||
"""
|
||||
logger.info(f"Generating image for text: {text}")
|
||||
# 1. Simulate image generation using Pillow
|
||||
try:
|
||||
img = Image.new("RGB", (400, 100), color=(73, 109, 137))
|
||||
draw = ImageDraw.Draw(img)
|
||||
try:
|
||||
font = ImageFont.truetype("arial.ttf", 15)
|
||||
except OSError:
|
||||
font = ImageFont.load_default()
|
||||
draw.text((10, 10), f"Generated from: {text}", fill=(255, 255, 0), font=font)
|
||||
async def text_to_img_kolors(text: str) -> str:
|
||||
"""(用来测试文件存储)使用Kolors模型生成图片, 会返回图片的URL"""
|
||||
|
||||
img_bytes = BytesIO()
|
||||
img.save(img_bytes, format="JPEG")
|
||||
img_bytes.seek(0)
|
||||
file_data = img_bytes.read()
|
||||
logger.info("Image data generated successfully.")
|
||||
url = "https://api.siliconflow.cn/v1/images/generations"
|
||||
|
||||
payload = {
|
||||
"model": "Kwai-Kolors/Kolors",
|
||||
"prompt": text,
|
||||
"image_size": "512x512",
|
||||
"batch_size": 1,
|
||||
"num_inference_steps": 20,
|
||||
"guidance_scale": 7.5
|
||||
}
|
||||
headers = {
|
||||
"Authorization": f"Bearer {os.getenv('SILICONFLOW_API_KEY')}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
response_json = response.json()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate image with Pillow: {e}")
|
||||
logger.error(f"Failed to generate image with Kolors: {e}")
|
||||
raise ValueError(f"Image generation failed: {e}")
|
||||
|
||||
try:
|
||||
image_url = response_json["images"][0]["url"]
|
||||
except (KeyError, IndexError, TypeError) as e:
|
||||
logger.error(f"Failed to parse image URL from Kolors response: {e}, {response_json=}")
|
||||
raise ValueError(f"Image URL extraction failed: {e}")
|
||||
|
||||
# 2. Upload to MinIO (Simplified)
|
||||
response = requests.get(image_url)
|
||||
file_data = response.content
|
||||
|
||||
image_url = upload_image_to_minio(data=file_data, file_extension="jpg")
|
||||
logger.info(f"Image uploaded. URL: {image_url}")
|
||||
return image_url
|
||||
@ -67,5 +75,5 @@ def get_tools() -> list[Any]:
|
||||
"""获取所有可运行的工具(给大模型使用)"""
|
||||
tools = get_buildin_tools()
|
||||
tools.append(calculator)
|
||||
tools.append(text_to_img)
|
||||
tools.append(text_to_img_kolors)
|
||||
return tools
|
||||
|
||||
@ -18,11 +18,12 @@ MCP_SERVERS = {
|
||||
"url": "https://remote.mcpservers.org/sequentialthinking/mcp",
|
||||
"transport": "streamable_http",
|
||||
},
|
||||
"time": {
|
||||
"command": "uvx",
|
||||
"args": ["mcp-server-time"],
|
||||
"transport": "stdio",
|
||||
},
|
||||
# 这些 stdio 的 MCP server 需要在本地启动,启动的时候需要安装对应的包,需要时间
|
||||
# "time": {
|
||||
# "command": "uvx",
|
||||
# "args": ["mcp-server-time"],
|
||||
# "transport": "stdio",
|
||||
# },
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -27,7 +27,8 @@ class _MinioClient:
|
||||
self.secret_key = os.getenv("MINIO_SECRET_KEY", "minioadmin")
|
||||
|
||||
if os.getenv("RUNNING_IN_DOCKER"):
|
||||
self.public_endpoint = f"localhost:{self.endpoint.split(':')[-1]}"
|
||||
host_ip = os.getenv("HOST_IP", "localhost")
|
||||
self.public_endpoint = f"{host_ip}:{self.endpoint.split(':')[-1]}"
|
||||
else:
|
||||
self.public_endpoint = self.endpoint
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user