Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Llama index fix #327

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions agenthub/app/agentchat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MessageList } from '@/components/agentchat/MessageList';
import axios from 'axios';
import { AgentCommand } from '@/components/chat/body/message-box';
import { baseUrl, serverUrl } from '@/lib/env';
import { generateSixDigitId } from '@/lib/utils';



Expand Down Expand Up @@ -94,7 +95,7 @@ const ChatInterface: React.FC = () => {
const handleSend = async (content: string, attachments: File[]) => {
if (content.trim() || attachments.length > 0) {
const newMessage: Message = {
id: Date.now(),
id: `${generateSixDigitId()}`,
text: content,
sender: 'user',
timestamp: new Date(),
Expand All @@ -103,7 +104,7 @@ const ChatInterface: React.FC = () => {
};
setMessages([...messages, newMessage]);

let messageId = Date.now();
let messageId = generateSixDigitId();

// Handle file uploads here (e.g., to a server)
const botMessage: Message = {
Expand All @@ -120,11 +121,13 @@ const ChatInterface: React.FC = () => {

setMessages(prevMessages => [...prevMessages].map(message => {
if (message.id == messageId) {
return { ...message, thinking: false };
return { ...message, thinking: false, text: res.content };
}
return res.content;
// return res.content;
return message;
}));
}

};

const addChat = () => {
Expand Down
8 changes: 2 additions & 6 deletions agenthub/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export const inDevEnvironment = !!process && process.env.NODE_ENV === 'development';
// export const serverUrl = inDevEnvironment ? 'http://localhost:8000' : 'https://myapp-y5z35kuonq-uk.a.run.app'
export const baseUrl = process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: 'https://my.aios.foundation';
export const baseUrl = inDevEnvironment ? 'http://localhost:3000' : 'https://my.aios.foundation'
// export const serverUrl = inDevEnvironment ? 'http://localhost:8000' : 'http://35.232.56.61:8000'
export const serverUrl = process.env.NODE_ENV === 'development'
? 'http://localhost:8000'
: 'https://api.aios.chat';
export const serverUrl = 'http://35.232.56.61:8000';

Empty file added cerebrum/__init__.py
Empty file.
150 changes: 150 additions & 0 deletions cerebrum/agents/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import os
import json
import time

from cerebrum.utils.chat import Query
from cerebrum.runtime.process import AgentProcessor
import importlib

class BaseAgent:
def __init__(self,
agent_name: str,
task_input: str,
config: dict):
# super().__init__()
self.agent_name = agent_name
self.config = config

self.tool_list = dict()
self.tools = []
self.tool_info = ([]) # simplified information of the tool: {"name": "xxx", "description": "xxx"}

self.load_tools(self.config.get('tools'))
self.rounds = 0

self.task_input = task_input
self.messages = []
self.workflow_mode = "manual" # (manual, automatic)

self.llm = None


def run(self):
# raise NotImplementedError
pass

def build_system_instruction(self):
pass


def check_workflow(self, message):
try:
# print(f"Workflow message: {message}")
workflow = json.loads(message)
if not isinstance(workflow, list):
return None

for step in workflow:
if "message" not in step or "tool_use" not in step:
return None

return workflow

except json.JSONDecodeError:
return None

def automatic_workflow(self):
for i in range(self.plan_max_fail_times):
response = AgentProcessor.process_response(query=Query(messages=self.messages, tools=None, message_return_type="json"), llm=self.llm)

workflow = self.check_workflow(response.response_message)

self.rounds += 1

if workflow:
return workflow

else:
self.messages.append(
{
"role": "assistant",
"content": f"Fail {i+1} times to generate a valid plan. I need to regenerate a plan",
}
)
return None

def manual_workflow(self):
pass

def check_path(self, tool_calls):
script_path = os.path.abspath(__file__)
save_dir = os.path.join(
os.path.dirname(script_path), "output"
) # modify the customized output path for saving outputs
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for tool_call in tool_calls:
try:
for k in tool_call["parameters"]:
if "path" in k:
path = tool_call["parameters"][k]
if not path.startswith(save_dir):
tool_call["parameters"][k] = os.path.join(
save_dir, os.path.basename(path)
)
except Exception:
continue
return tool_calls

def snake_to_camel(self, snake_str):
components = snake_str.split("_")
return "".join(x.title() for x in components)

def load_tools(self, tool_names):
if tool_names == None:
return

for tool_name in tool_names:
org, name = tool_name.split("/")
module_name = ".".join(["cerebrum", "tools", org, name])
class_name = self.snake_to_camel(name)
tool_module = importlib.import_module(module_name)
tool_class = getattr(tool_module, class_name)
self.tool_list[name] = tool_class()
tool_format = tool_class().get_tool_call_format()
self.tools.append(tool_format)
self.tool_info.append(
{
"name": tool_format["function"]["name"],
"description": tool_format["function"]["description"],
}
)

def pre_select_tools(self, tool_names):
pre_selected_tools = []
for tool_name in tool_names:
for tool in self.tools:
if tool["function"]["name"] == tool_name:
pre_selected_tools.append(tool)
break

return pre_selected_tools

def create_agent_request(self, query):
agent_process = self.agent_process_factory.activate_agent_process(
agent_name=self.agent_name, query=query
)
agent_process.set_created_time(time.time())
# print("Already put into the queue")
return agent_process

def set_aid(self, aid):
self.aid = aid

def get_aid(self):
return self.aid

def get_agent_name(self):
return self.agent_name


Loading