3v324v23 commited on
Commit
19e7545
·
1 Parent(s): fb8ad78

自己用gemni寫image tool

Browse files
Files changed (2) hide show
  1. litellm_endpoint.py +32 -4
  2. requirements.txt +2 -1
litellm_endpoint.py CHANGED
@@ -2,11 +2,15 @@ import os
2
  import datetime
3
  import pytz
4
  import yaml
 
 
 
 
5
 
6
  from smolagents import (
7
  CodeAgent,
8
  DuckDuckGoSearchTool,
9
- load_tool,
10
  tool,
11
  LiteLLMModel,
12
  )
@@ -43,7 +47,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
43
 
44
 
45
  # 初始化工具
46
- search_tool = DuckDuckGoSearchTool()
47
  final_answer = FinalAnswerTool()
48
 
49
  # 官方文件寫法
@@ -54,7 +58,31 @@ model = LiteLLMModel(
54
  api_key=os.getenv("GEMINI_API_KEY"))
55
 
56
  # 載入圖像產生工具
57
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # 載入 YAML Prompt 模板
60
  with open("prompts.yaml", 'r') as stream:
@@ -63,7 +91,7 @@ with open("prompts.yaml", 'r') as stream:
63
  # 設定代理人
64
  agent = CodeAgent(
65
  model=model,
66
- tools=[final_answer, search_tool, image_generation_tool, get_current_time_in_timezone], # Add your tools here (don't remove final answer)
67
  max_steps=6,
68
  verbosity_level=1,
69
  grammar=None,
 
2
  import datetime
3
  import pytz
4
  import yaml
5
+ from PIL import Image
6
+ from google import genai
7
+ from google.genai import types
8
+ from io import BytesIO
9
 
10
  from smolagents import (
11
  CodeAgent,
12
  DuckDuckGoSearchTool,
13
+ # load_tool,
14
  tool,
15
  LiteLLMModel,
16
  )
 
47
 
48
 
49
  # 初始化工具
50
+ web_search = DuckDuckGoSearchTool()
51
  final_answer = FinalAnswerTool()
52
 
53
  # 官方文件寫法
 
58
  api_key=os.getenv("GEMINI_API_KEY"))
59
 
60
  # 載入圖像產生工具
61
+ # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
62
+
63
+ @tool
64
+ def image_generation_tool(prompt: str) -> Image.Image:
65
+ """Generates an image based on the provided prompt.
66
+
67
+ Args:
68
+ prompt: The prompt for the image generation.
69
+ """
70
+ # Placeholder for actual image generation logic
71
+ client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
72
+ response = client.models.generate_content(
73
+ model="gemini-2.0-flash-exp-image-generation",
74
+ contents=prompt,
75
+ config=types.GenerateContentConfig(
76
+ response_modalities=["TEXT", "IMAGE"]
77
+ ),
78
+ )
79
+
80
+ # 處理回應中的圖片
81
+ for part in response.candidates[0].content.parts:
82
+ if part.inline_data is not None:
83
+ image = Image.open(BytesIO(part.inline_data.data))
84
+ return image
85
+
86
 
87
  # 載入 YAML Prompt 模板
88
  with open("prompts.yaml", 'r') as stream:
 
91
  # 設定代理人
92
  agent = CodeAgent(
93
  model=model,
94
+ tools=[final_answer, web_search, image_generation_tool, get_current_time_in_timezone], # Add your tools here (don't remove final answer)
95
  max_steps=6,
96
  verbosity_level=1,
97
  grammar=None,
requirements.txt CHANGED
@@ -4,4 +4,5 @@ requests
4
  duckduckgo_search
5
  pandas
6
  smolagents[openai]
7
- smolagents[litellm]
 
 
4
  duckduckgo_search
5
  pandas
6
  smolagents[openai]
7
+ smolagents[litellm]
8
+ google-genai