Skip to content

Commit

Permalink
Update llm
Browse files Browse the repository at this point in the history
  • Loading branch information
dewmal committed Jul 2, 2024
1 parent d937498 commit a52f600
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 6 deletions.
11 changes: 10 additions & 1 deletion bindings/ceylon/ceylon/llm/llm_agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import pickle
import random
from collections import deque
from typing import List

Expand All @@ -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

Expand Down Expand Up @@ -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}
Expand All @@ -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])

Expand All @@ -101,13 +107,16 @@ 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))

await self.update_status(next_agent)

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:
Expand Down
Empty file.
Empty file.
91 changes: 91 additions & 0 deletions bindings/ceylon/examples/blog_writer/editor_panel.py
Original file line number Diff line number Diff line change
@@ -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())
16 changes: 11 additions & 5 deletions bindings/ceylon/tests/llm_agen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -38,14 +38,15 @@ async def main():
llm=llm_lib,
responsibilities=[
"Conducting thorough and accurate research to support content creation.",
"summarize with source references"

],
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."
"Must summarize output with source references."
],
tools=[
SearchTool()
Expand All @@ -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.",
Expand Down Expand Up @@ -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={
Expand Down

0 comments on commit a52f600

Please sign in to comment.