From a52f60005b9e6679b34388154d91e9a1da91b546 Mon Sep 17 00:00:00 2001 From: dewmal Date: Tue, 2 Jul 2024 22:18:56 +0530 Subject: [PATCH] Update llm --- bindings/ceylon/ceylon/llm/llm_agent.py | 11 ++- bindings/ceylon/examples/__init__.py | 0 .../ceylon/examples/blog_writer/__init__.py | 0 .../examples/blog_writer/editor_panel.py | 91 +++++++++++++++++++ bindings/ceylon/tests/llm_agen_test.py | 16 +++- 5 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 bindings/ceylon/examples/__init__.py create mode 100644 bindings/ceylon/examples/blog_writer/__init__.py create mode 100644 bindings/ceylon/examples/blog_writer/editor_panel.py diff --git a/bindings/ceylon/ceylon/llm/llm_agent.py b/bindings/ceylon/ceylon/llm/llm_agent.py index e9d56b3c..c8ff8a5c 100644 --- a/bindings/ceylon/ceylon/llm/llm_agent.py +++ b/bindings/ceylon/ceylon/llm/llm_agent.py @@ -1,4 +1,6 @@ +import asyncio import pickle +import random from collections import deque from typing import List @@ -21,6 +23,7 @@ class LLMAgentResponse: class LLMAgent(AgentCore, MessageHandler, Processor): tools: list[StructuredTool] network_graph: nx.DiGraph + network_graph_original: nx.DiGraph queue: deque original_goal = None @@ -53,7 +56,7 @@ async def on_message(self, agent_id, data, time): next_agent = self.get_next_agent() if next_agent == definition.name: - dependencies = list(self.network_graph.predecessors(next_agent)) + dependencies = list(self.network_graph_original.predecessors(next_agent)) print("Dependencies are:", dependencies, "for", next_agent) only_dependencies = {dt.agent_name: dt for dt in self.agent_replies if dt.agent_name in dependencies} @@ -79,10 +82,13 @@ async def run(self, inputs): def _initialize_graph(self, network): # Add nodes and edges based on the agents and their dependencies for agent, dependencies in network.items(): + print(agent) self.network_graph.add_node(agent) for dependency in dependencies: self.network_graph.add_edge(dependency, agent) + self.network_graph_original = self.network_graph.copy() + # Initialize the queue with nodes that have no dependencies (indegree 0) self.queue.extend([node for node in self.network_graph if self.network_graph.in_degree(node) == 0]) @@ -101,6 +107,7 @@ async def execute(self, input): result = process_agent_request(self.llm, input, definition, tools=self.tools) # result = f"{definition.name} executed successfully" response = LLMAgentResponse(agent_id=definition.id, agent_name=definition.name, response=result) + await asyncio.sleep(random.randint(1, 10)) await self.broadcast(pickle.dumps(response)) @@ -108,6 +115,8 @@ async def execute(self, input): next_agent = self.get_next_agent() print("Next agent will be:", next_agent) + else: + print("Not executing", definition.name, "as it is not the next agent in the queue.") async def update_status(self, agent): if agent not in self.queue: diff --git a/bindings/ceylon/examples/__init__.py b/bindings/ceylon/examples/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bindings/ceylon/examples/blog_writer/__init__.py b/bindings/ceylon/examples/blog_writer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bindings/ceylon/examples/blog_writer/editor_panel.py b/bindings/ceylon/examples/blog_writer/editor_panel.py new file mode 100644 index 00000000..a9d67953 --- /dev/null +++ b/bindings/ceylon/examples/blog_writer/editor_panel.py @@ -0,0 +1,91 @@ +import asyncio + +from duckduckgo_search import DDGS +from langchain_community.chat_models import ChatOllama, ChatOpenAI +from langchain_core.tools import StructuredTool + +from ceylon import AgentRunner +from ceylon.llm.llm_agent import LLMAgent +from ceylon.tools.file_publisher_tool import FilePublisherTool +from ceylon.tools.search_tool import SearchTool + + +async def main(): + runner = AgentRunner(workspace_name="ceylon-ai") + # llm_lib = ChatOllama(model="phi3:instruct") + llm_lib = ChatOllama(model="gemma2:latest") + # llm_lib = ChatOpenAI(model="gpt-4o") + runner.register_agent(LLMAgent( + name="name_chooser", + position="Select File name", + llm=llm_lib, + responsibilities=["Create high-quality, SEO friendly file name."], + instructions=[ + "Easy to read and understand." + ] + )) + + runner.register_agent(LLMAgent( + name="researcher", + position="Content Researcher", + llm=llm_lib, + responsibilities=[ + "Conducting thorough and accurate research to support content creation.", + + ], + instructions=[ + "Must only Find the most relevant 2 or 3 sources." + "Find credible sources, verify information, and provide comprehensive and relevant " + "data while ensuring ethical " + "standards and privacy are maintained.", + "Must summarize output without source references." + ], + tools=[ + SearchTool() + ] + )) + # + + runner.register_agent(LLMAgent( + name="writer", + position="Assistant Writer", + llm=llm_lib, + responsibilities=["Create high-quality, original content that matches the audience's tone and style."], + instructions=[ + "Ensure clarity, accuracy, and proper formatting while respecting ethical guidelines and privacy."] + )) + # + runner.register_agent(LLMAgent( + name="publisher", + position="Content Publisher", + llm=llm_lib, + responsibilities=[ + "Publish the finalized content using the publishing tools and platforms specified by the writer.", + ], + instructions=[ + "Publish it as finalized and polished content", + ], + tools=[ + FilePublisherTool() + ] + )) + + await runner.run( + { + "request": "I want to create a blog post", + "title": "How to use AI for Machine Learning", + "tone": "informal", + "length": "large", + "style": "creative" + }, + network={ + "name_chooser": [], + "researcher": [], + "writer": ["researcher"], + "publisher": ["writer", "name_chooser"] + } + ) + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/bindings/ceylon/tests/llm_agen_test.py b/bindings/ceylon/tests/llm_agen_test.py index 88ffd06c..22d13525 100644 --- a/bindings/ceylon/tests/llm_agen_test.py +++ b/bindings/ceylon/tests/llm_agen_test.py @@ -12,8 +12,8 @@ async def main(): runner = AgentRunner(workspace_name="ceylon-ai") - # llm_lib = ChatOllama(model="llama3:instruct") - llm_lib = ChatOpenAI(model="gpt-4o") + llm_lib = ChatOllama(model="llama3:instruct") + # llm_lib = ChatOpenAI(model="gpt-4o") runner.register_agent(LLMAgent( name="writer", position="Assistant Writer", @@ -38,6 +38,7 @@ async def main(): llm=llm_lib, responsibilities=[ "Conducting thorough and accurate research to support content creation.", + "summarize with source references" ], instructions=[ @@ -45,7 +46,7 @@ async def main(): "Find credible sources, verify information, and provide comprehensive and relevant " "data while ensuring ethical " "standards and privacy are maintained.", - "Must summarize output without source references." + "Must summarize output with source references." ], tools=[ SearchTool() @@ -57,7 +58,12 @@ async def main(): position="Content Editor", llm=llm_lib, responsibilities=[ - "Review and refine content to ensure it meets quality standards and aligns with the editorial guidelines."], + "Review and refine content to ensure it meets quality standards and aligns with the editorial guidelines.", + "Write content in a clear and concise manner.", + "Ensure the content is appropriate for the target audience.", + "Ensure the content is engaging and maintains the intended tone and style.", + "Include source references when appropriate." + ], instructions=[ "Check for grammatical errors, clarity, and coherence.", "Ensure the content is engaging and maintains the intended tone and style.", @@ -85,7 +91,7 @@ async def main(): "request": "I want to create a blog post", "title": "How to use AI for Machine Learning", "tone": "informal", - "length": "short", + "length": "large", "style": "creative" }, network={