File size: 4,353 Bytes
277f3d3
c19d193
277f3d3
6aae614
9b5b26a
 
277f3d3
30dc270
277f3d3
30dc270
 
277f3d3
30dc270
b5edabf
277f3d3
9b5b26a
277f3d3
 
 
06a994d
277f3d3
 
 
 
 
06a994d
277f3d3
9b5b26a
277f3d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a7385
 
 
 
 
 
 
 
 
 
 
 
277f3d3
 
e4a7385
277f3d3
9b5b26a
972e2ed
e4a7385
 
 
 
 
 
 
 
 
 
 
277f3d3
8c01ffb
e4a7385
277f3d3
6aae614
ae7a494
277f3d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fe992b
db7705f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import json
import yaml
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# Load prompt templates from prompts.yaml
try:
    with open("prompts.yaml", 'r') as stream:
        prompt_templates = yaml.safe_load(stream)
except Exception as e:
    print(f"⚠️ Error loading prompt templates: {e}")
    prompt_templates = {}

# Research Tool (Limits articles to avoid hitting API limits)
@tool
def limited_research_tool(query: str, max_articles: int = 3) -> str:
    """Fetches only the first few research articles to avoid search limit errors.

    Args:
        query: The research topic to search online.
        max_articles: Number of articles to retrieve and summarize (default: 3).

    Returns:
        A summarized version of the key insights from the articles.
    """
    print(f"🔍 Researching: {query} (Max {max_articles} articles)")

    research_tool = DuckDuckGoSearchTool()  # Ensure tool is initialized

    try:
        # Get search results (limit articles)
        search_results = research_tool.run(query)[:max_articles]

        # Summarize articles (Placeholder summarizer)
        summaries = [f"Summary of {article}" for article in search_results]

        return "\n\n".join(summaries) if summaries else "No useful research found."

    except Exception as e:
        print(f"❌ Research failed: {e}")
        return "Research unavailable at the moment."

# Writer Tool (Formats research into an article draft)
@tool
def writer_tool(research_data: str) -> str:
    """
    Creates a structured article draft based on research data.

    Args:
        research_data: A string containing summarized research insights, typically 
                       generated by the limited_research_tool function. Each summary 
                       is separated by two newline characters and represents a key 
                       point or source.

    Returns:
        A draft article in string format, structured using the provided research data.
    """
    return f"📄 Draft article:\n{research_data}"


# Editor Tool (Refines and finalizes the article)
@tool
def editor_tool(draft: str) -> str:
    """
    Refines and enhances the draft article.

    Args:
        draft: A string representing the initial draft of an article, usually generated 
               by the writer_tool. This draft includes summarized research content 
               and serves as the basis for polishing the final article.

    Returns:
        A string containing the improved and refined version of the article.
    """
    return f"✍️ Edited article:\n{draft}"


# Final Answer Tool (Ensures structured output)
final_answer = FinalAnswerTool()

# Define LLM Model
model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
)

# Researcher Agent (Uses Limited Research Tool)
researcher = CodeAgent(
    model=model,
    tools=[limited_research_tool],
    max_steps=6,
    verbosity_level=2,
    name="Researcher",
    description=prompt_templates.get("managed_agent", {}).get("researcher", {}).get("task", "Research topic details."),
    prompt_templates=prompt_templates,
)

# Writer Agent
writer = CodeAgent(
    model=model,
    tools=[writer_tool],
    max_steps=6,
    verbosity_level=2,
    name="Writer",
    description=prompt_templates.get("managed_agent", {}).get("writer", {}).get("task", "Write an article draft."),
    prompt_templates=prompt_templates,
)

# Editor Agent (Includes Final Answer Tool)
editor = CodeAgent(
    model=model,
    tools=[editor_tool, final_answer],
    max_steps=6,
    verbosity_level=2,
    name="Editor",
    description=prompt_templates.get("managed_agent", {}).get("editor", {}).get("task", "Edit and finalize an article."),
    prompt_templates=prompt_templates,
)

# Orchestrator Agent (Manages Research, Writing, and Editing)
orchestrator_agent = CodeAgent(
    model=model,
    tools=[limited_research_tool, writer_tool, editor_tool, final_answer],
    max_steps=6,
    verbosity_level=2,
    name="Orchestrator",
    description="An agent that coordinates research, writing, and editing.",
    prompt_templates=prompt_templates,
)

# Launch Gradio UI
print("🚀 Launching Gradio UI...")
GradioUI(orchestrator_agent).launch()