huchiahsi commited on
Commit
f22cab9
·
1 Parent(s): beba4e2

openai_endpoint上傳

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +9 -1
  3. openai_endpoint.py +77 -0
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: pink
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.25.2
8
- app_file: app.py
9
  pinned: false
10
  tags:
11
  - smolagents
 
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.25.2
8
+ app_file: openai_endpoint.py
9
  pinned: false
10
  tags:
11
  - smolagents
app.py CHANGED
@@ -1,4 +1,12 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
 
 
 
 
 
 
 
2
  import datetime
3
  import requests
4
  import pytz
 
1
+ # === SmolAgents範例 ===
2
+ # 這個範例展示了如何使用 SmolAgents 來創建一個簡單的應用程式,該應用程式可以生成圖像並回答問題。
3
+ # 這個範例使用了 Hugging Face 的 Qwen2.5-Coder-32B-Instruct 模型來生成圖像和回答問題。
4
+ # 這個範例使用了 Gradio 來創建一個簡單的用戶界面,允許用戶輸入問題並獲得回答。
5
+ # 這個範例使用了 DuckDuckGoSearchTool 來搜索問題的答案。
6
+ # 這個範例使用了 pytz 來獲取當前的本地時間。
7
+ # SmolAgents官方文件:https://huggingface.co/docs/smolagents/v1.14.0/en/reference/models
8
+
9
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel,load_tool,tool
10
  import datetime
11
  import requests
12
  import pytz
openai_endpoint.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ OpenAIServerModel
12
+ )
13
+
14
+ from tools.final_answer import FinalAnswerTool
15
+ from Gradio_UI import GradioUI
16
+
17
+ # 自訂工具:展示結構
18
+ @tool
19
+ def my_custom_tool(arg1: str, arg2: int) -> str:
20
+ """A tool that does nothing yet.
21
+
22
+ Args:
23
+ arg1: The first argument.
24
+ arg2: The second argument.
25
+ """
26
+ return "What magic will you build?"
27
+
28
+
29
+ # 自訂工具:取得當地時間
30
+ @tool
31
+ def get_current_time_in_timezone(timezone: str) -> str:
32
+ """Fetches the current local time in a specified timezone.
33
+
34
+ Args:
35
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
36
+ """
37
+ try:
38
+ tz = pytz.timezone(timezone)
39
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
40
+ return f"The current local time in {timezone} is: {local_time}"
41
+ except Exception as e:
42
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
43
+
44
+
45
+ # 初始化工具
46
+ search_tool = DuckDuckGoSearchTool()
47
+ final_answer = FinalAnswerTool()
48
+
49
+ # 初始化 OpenAI 模型
50
+ model = OpenAIServerModel(
51
+ model_id="gpt-4o",
52
+ api_base="https://api.openai.com/v1",
53
+ api_key=os.environ["OPENAI_API_KEY"],
54
+ )
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:
61
+ prompt_templates = yaml.safe_load(stream)
62
+
63
+ # 設定代理人
64
+ agent = CodeAgent(
65
+ model=model,
66
+ tools=[final_answer, search_tool, image_generation_tool], # Add your tools here (don't remove final answer)
67
+ max_steps=6,
68
+ verbosity_level=1,
69
+ grammar=None,
70
+ planning_interval=None,
71
+ name=None,
72
+ description=None,
73
+ prompt_templates=prompt_templates,
74
+ )
75
+
76
+ # 啟動 Gradio 介面
77
+ GradioUI(agent).launch()