Skip to content

Commit

Permalink
refactor openai client
Browse files Browse the repository at this point in the history
  • Loading branch information
patel-lyzr committed Jan 28, 2024
1 parent 24ceab9 commit fde6c60
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
dist/
build/
.vscode/
38 changes: 38 additions & 0 deletions cookbook/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import nest_asyncio
from lyzr_experimental_automata import Agent, Task, State

os.environ["OPENAI_API_KEY"] = "sk-"

nest_asyncio.apply()

agent1 = Agent(persona="Marketing Consultant")
agent2 = Agent(persona="Tweet Generator")
agent3 = Agent(persona="Linkedin Post Creator")

task1 = Task(
instructions="Do research and pull out interesting marketing tips for SaaS companies. The research articles should not be more than 500 words.",
desired_output="Ensure that you bring the best content from the likes of HBS, YCombinator",
agent=agent1,
display_output="yes",
)

task2 = Task(
instructions="Use the research material provided and write 5 engaging tweets. Display only the tweets. No explanation or additional comments required.",
desired_output="Ensure that the tweets are as engaging as if it was written by the best influencer in the world",
agent=agent2,
display_output="yes",
dependencies=[task1],
)

task3 = Task(
instructions="Use the research material provided and write 1 short form LinkedIn post. Display only the LinkedIn post. No explanation or additional comments required.",
desired_output="Ensure that the post is as if it was written by the best influencer in the world",
agent=agent3,
display_output="yes",
dependencies=[task1],
)

output = State([task1, task2, task3])

print(output)
74 changes: 74 additions & 0 deletions lyzr_experimental_automata.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
Metadata-Version: 2.1
Name: lyzr-experimental-automata
Version: 0.2.0
Summary: A prompt based agent workflow that integrates with other Lyzr agents
Home-page: https://github.com/lyzrcore/lyzr-experimental-automata
Author: Siva Surendira
Author-email: siva@lyzr.ai
License: MIT
Description-Content-Type: check Readme file
License-File: LICENSE
Requires-Dist: openai
Requires-Dist: asyncio

# lyzr-experimental-automata
The version 0.2 is a prompt based agent workflow that integrates with other Lyzr agents

# Lyzr Automata - Parallel-Processing Agent Automation Framework

Lyzr Automata is a sophisticated parallel-processing agent automation framework designed to enhance workflow efficiency and effectiveness. It enables a seamless transition between states, leverages recursive checks for continual improvement, and ensures the achievement of end goals in line with assigned objectives.

## How to Install

Get started with Lyzr Automata by installing the experimental package using pip:

pip install lyzr-experimental-automata

## Configuring Agents

Begin by configuring your agents and assigning them unique personas:

```python
agent1 = Agent(persona="enter the persona of agent1")
agent2 = Agent(persona="enter the persona of agent2")
agent3 = Agent(persona="enter the persona of agent3")

Example Configuration:

agent1 = Agent(persona="Marketing Consultant")
agent2 = Agent(persona="Tweet Generator")
agent3 = Agent(persona="Linkedin Post Creator")

Creating Tasks
Create tasks by providing specific instructions and desired outcomes. Assign these tasks to your agents:

task1 = Task("enter the instructions", "enter the desired outcome", agent1, display_output='no')

Task1 is the initial task in the workflow. You can control the visibility of its output by setting display_output to either 'yes' or 'no'.

Example Task 1:

task1 = Task("Do research and pull out interesting marketing tips for SaaS companies. The research articles should not be more than 500 words.", "Ensure that you bring the best content from the likes of HBS, YCombinator", agent1, display_output='no')

Setting Up Dependencies
Leverage the multi-thread, parallel-processing capabilities of Lyzr Automata by specifying task dependencies:

task2 = Task("enter the instructions", "enter the desired outcome", agent1, display_output='yes', dependencies=[task1])

Example Task 2:

task2 = Task("Use the research material provided and write 5 engaging tweets. Display only the tweets. No explanation or additional comments required.", "Ensure that the tweets are as engaging as if it was written by the best influencer in the world", agent2, display_output='yes', dependencies=[task1])

Continue adding tasks as required, defining their dependencies to optimize parallel processing.

Further Tasks
Add additional tasks (like Task 3) and define their dependencies for continued workflow.

task3 = Task("enter the instructions", "enter the desired outcome", agent1, display_output='yes', dependencies=[task1])

Example Task 3:

task3 = Task("Use the research material provided and write 1 short form LinkedIn post. Display only the LinkedIn post. No explanation or additional comments required.", "Ensure that the post is as if it was written by the best influencer in the world", agent3, display_output='yes', dependencies=[task1])

Lyzr Automata is your go-to solution for creating an efficient, automated workflow powered by the capabilities of OpenAI.

10 changes: 10 additions & 0 deletions lyzr_experimental_automata.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
LICENSE
README.md
setup.py
lyzr_experimental_automata/__init__.py
lyzr_experimental_automata/automata.py
lyzr_experimental_automata.egg-info/PKG-INFO
lyzr_experimental_automata.egg-info/SOURCES.txt
lyzr_experimental_automata.egg-info/dependency_links.txt
lyzr_experimental_automata.egg-info/requires.txt
lyzr_experimental_automata.egg-info/top_level.txt
1 change: 1 addition & 0 deletions lyzr_experimental_automata.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions lyzr_experimental_automata.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
openai
asyncio
1 change: 1 addition & 0 deletions lyzr_experimental_automata.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lyzr_experimental_automata
2 changes: 1 addition & 1 deletion lyzr_experimental_automata/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .automata import Agent, Task, run_state
from lyzr_experimental_automata.automata import Agent, Task, State
65 changes: 45 additions & 20 deletions lyzr_experimental_automata/automata.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,91 @@
import os
import asyncio
import openai
from openai import OpenAI


class Agent:
def __init__(self, persona):
self.persona = persona


class Task:
def __init__(self, instructions, desired_output, agent, display_output='no', dependencies=None,
model="gpt-4-turbo-preview", temperature=1, max_tokens=1000, top_p=1,
frequency_penalty=0, presence_penalty=0):
def __init__(
self,
instructions,
desired_output,
agent,
display_output="no",
dependencies=None,
model="gpt-4-turbo-preview",
temperature=1,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
):
self.instructions = instructions
self.desired_output = desired_output
self.agent = agent
self.display_output = display_output.lower() == 'yes'
self.display_output = display_output.lower() == "yes"
self.dependencies = dependencies if dependencies else []
self.model = model
self.temperature = temperature
self.max_tokens = max_tokens
self.top_p = top_p
self.frequency_penalty = frequency_penalty
self.presence_penalty = presence_penalty
self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

async def execute(self, input_from_previous_task=""):
persona_with_prefix = f"Your role is a {self.agent.persona}"
prompt = (f"Instructions {self.instructions} Desired outcome: {self.desired_output} Input: {input_from_previous_task}")
prompt = f"Instructions {self.instructions} Desired outcome: {self.desired_output} Input: {input_from_previous_task}"

loop = asyncio.get_event_loop()
response = await loop.run_in_executor(None, lambda: client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": persona_with_prefix},
{"role": "user", "content": prompt}
],
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=self.top_p,
frequency_penalty=self.frequency_penalty,
presence_penalty=self.presence_penalty
))
response = await loop.run_in_executor(
None,
lambda: self.client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": persona_with_prefix},
{"role": "user", "content": prompt},
],
temperature=self.temperature,
max_tokens=self.max_tokens,
top_p=self.top_p,
frequency_penalty=self.frequency_penalty,
presence_penalty=self.presence_penalty,
),
)
return response.choices[0].message.content


async def _execute_task(task, task_to_future, outputs):
for dependency in task.dependencies:
await task_to_future[dependency]

input_from_previous_task = " ".join(outputs.get(dep, "") for dep in task.dependencies)
input_from_previous_task = " ".join(
outputs.get(dep, "") for dep in task.dependencies
)
output = await task.execute(input_from_previous_task)

outputs[task] = output
if task.display_output:
print("Task Output:", output)


async def _run_tasks(tasks):
outputs = {}
task_to_future = {}

for task in tasks:
task_to_future[task] = asyncio.create_task(_execute_task(task, task_to_future, outputs))
task_to_future[task] = asyncio.create_task(
_execute_task(task, task_to_future, outputs)
)

await asyncio.gather(*task_to_future.values())

return " ".join(outputs[task] for task in tasks)


def State(tasks):
return asyncio.run(_run_tasks(tasks))

0 comments on commit fde6c60

Please sign in to comment.