Spaces:
Runtime error
Runtime error
| import time | |
| import gradio as gr | |
| from agno.agent import Agent | |
| from agno.models.google.gemini import Gemini | |
| def llm(message, history: list[dict], api_key, num_conversation=3): | |
| model = Gemini(api_key=api_key) | |
| agent = Agent(model=model, | |
| # tools=[WebsiteTools(), HackerNewsTools()], | |
| description="Bạn là một trợ lý AI hữu ích và thông minh. Dưới đây là lịch sử hội thoại giữa bạn và người dùng:", | |
| instructions=[f"Sử dụng kiến thức chuyên môn để trả lời câu hỏi hiện tại", | |
| "Xem xét ngữ cảnh từ các câu hỏi và trả lời trước đó", | |
| "Khi được hỏi về tin tức mới nhất mà không cung cấp thông tin cụ thể thì mặc định chọn 3 tin mới nhất từ HackerNews"], | |
| show_tool_calls=True, markdown=True) | |
| history.append({ | |
| "question": message["text"], | |
| "answer": "" | |
| }) | |
| conversation = "" | |
| for entry in history[-num_conversation:]: | |
| conversation += f"User: {entry['question']}\n" | |
| conversation += f"Chatbot: {entry['answer']}\n\n" | |
| result = agent.run(conversation).content | |
| history[-1]["answer"] = result | |
| return result | |
| chat_history = [] | |
| def process(message, history): | |
| key = "AIzaSyBLLXxkA0Ij5iHr6tXgIcpwzCBANJQn4_o" | |
| result = llm(message, chat_history, api_key=key, num_conversation=3) | |
| temp = "" | |
| for i in str(result).split(): | |
| time.sleep(0.05) | |
| temp += i + " " | |
| yield temp | |
| gr.ChatInterface( | |
| multimodal=True, | |
| fn=process, | |
| type="messages", | |
| fill_width=True, | |
| save_history=True | |
| ).launch() | |