File size: 4,202 Bytes
6bcd0c6
 
6eb6008
6bcd0c6
6eb6008
6bcd0c6
 
6eb6008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bcd0c6
 
 
 
 
 
 
 
6eb6008
6bcd0c6
6eb6008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bcd0c6
6eb6008
 
 
 
 
6bcd0c6
6eb6008
6bcd0c6
6eb6008
6bcd0c6
6eb6008
 
 
 
 
 
 
 
 
 
6bcd0c6
6eb6008
6bcd0c6
 
 
6eb6008
 
 
 
6bcd0c6
 
 
 
 
 
 
 
 
6eb6008
 
 
 
 
 
 
6bcd0c6
 
 
 
6eb6008
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
import gradio as gr
from huggingface_hub import InferenceClient
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, VisitWebpageTool, HfApiModel

# Initialize HuggingFace client
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

# Create the smolagents agent (without UserInputTool since we'll get input from Gradio)
agent = CodeAgent(
    tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), FinalAnswerTool()],
    model=HfApiModel(),
    max_steps=8,
    verbosity_level=1
)

# Function to perform web research with a provided query
def research_with_query(query):
    result = agent.run(f"""
    Think step by step:

    1. The user has asked about: "{query}"
    2. Use the DuckDuckGoSearchTool to search the web for information about this query.
    3. From the search results, identify 1-2 relevant webpage URLs that might contain detailed information.
    4. Use the VisitWebpageTool to visit each identified webpage and extract its content.
    5. Combine the information from the search results and webpage visits.
    6. Create a comprehensive bullet point summary of all collected information.
    7. Each bullet point should start with "• " and be on a new line.
    8. Use the FinalAnswerTool to present your bullet-point summary as the final answer.

    Make sure your bullet points are clear, well-organized, and directly relevant to the user's query.
    Include the most important and factual information from your research.
    """)
    return result

def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
    use_web_search,
):
    # Check if web search is enabled and message starts with a research request
    if use_web_search and message.strip().lower().startswith(("search:", "research:", "find info:")):
        query = message.split(":", 1)[1].strip()
        
        yield "Searching the web for information about your query. This may take a moment..."
        
        try:
            # Perform the web search and get bullet point summary
            research_results = research_with_query(query)
            
            # Return the research results
            yield f"Here's what I found about '{query}':\n\n{research_results}"
        except Exception as e:
            yield f"Sorry, I encountered an error while searching the web: {str(e)}"
    else:
        # Regular chat completion for normal messages
        messages = [{"role": "system", "content": system_message}]

        for val in history:
            if val[0]:
                messages.append({"role": "user", "content": val[0]})
            if val[1]:
                messages.append({"role": "assistant", "content": val[1]})

        messages.append({"role": "user", "content": message})

        response = ""

        for message in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            token = message.choices[0].delta.content
            response += token
            yield response

# Create the Gradio interface
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Textbox(
            value="You are a helpful assistant. When users ask you to search for information with 'search:', 'research:', or 'find info:', you will search the web for them.", 
            label="System message"
        ),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
        gr.Checkbox(value=True, label="Enable web search (use 'search:', 'research:', or 'find info:' to search)")
    ],
    examples=[
        ["search: latest developments in quantum computing"],
        ["research: climate change impacts in 2023"],
        ["find info: benefits of meditation"],
        ["Hello! How are you today?"]
    ],
)

if __name__ == "__main__":
    demo.launch()