Skip to content

Commit

Permalink
add binary classify agent + example and add separate examples documen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
createreadupdate committed Sep 13, 2024
1 parent 3eb3d75 commit 002b35a
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ For more detailed information, please refer to the [INSTALL.md](INSTALL.md) file

## Examples

You can find more detailed examples in our [Examples Documentation](docs/EXAMPLES.md).

### Example 1: Filter List

Run the **filter list** example to see how AgentM filters a list of items based on whether they are considered healthy snacks:
Expand Down Expand Up @@ -101,4 +103,4 @@ Thus, the final answer is:
\[
\sqrt{144} = 12
\]
```
```
111 changes: 111 additions & 0 deletions docs/EXAMPLES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# AgentM Examples

This document contains detailed examples of how to use the micro-agents in AgentM. Here, we walk through practical use cases and outputs to help you understand how the library functions in various scenarios.

For more general information and a quick overview, refer to the [README](../README.md).

## Example 1: Filter List

Run the **filter list** example to see how AgentM filters a list of items based on whether they are considered healthy snacks:

```bash
python examples/filter_list_example.py
```

### Sample Output:
```bash
Original list: ['Apple', 'Chocolate bar', 'Carrot', 'Chips', 'Orange']

Filtered results:

{'explanation': 'The apple is a healthy snack option, as it is low in calories, rich in fiber, and provides essential vitamins such as vitamin C.', 'remove_item': False}
{'explanation': 'A chocolate bar is generally considered an unhealthy snack because it is high in sugar and saturated fats, which can contribute to weight gain and other health issues if consumed in excess.', 'remove_item': True}
{'explanation': 'Carrots are a healthy snack option, as they are low in calories, high in fiber, and rich in vitamins and minerals.', 'remove_item': False}
{'explanation': 'Chips are typically high in unhealthy fats, sodium, and calories, making them a less nutritious snack choice...', 'remove_item': True}
{'explanation': 'The orange is a healthy snack option, as it is low in calories and high in vitamin C and dietary fiber, making it a nutritious choice.', 'remove_item': False}

Final Filtered List: ['Apple', 'Carrot', 'Orange']
```

## Example 2: Sort List

Run the **sort list** example to see how AgentM sorts a list of items using LLM intelligence:

```bash
python examples/sort_list_example.py
```

### Sample Output:
```bash
2024-09-11 10:46:22,401 - INFO - Sending batch comparison request with prompt: Compare Apple and Orange and return the items in the correct order as 'item1,item2'.
2024-09-11 10:46:22,729 - INFO - Received response: Apple,Orange
2024-09-11 10:46:22,730 - INFO - Sending batch comparison request with prompt: Compare Grape and Pineapple and return the items in the correct order as 'item1,item2'.
2024-09-11 10:46:23,094 - INFO - Received response: Grape,Pineapple
2024-09-11 10:46:23,094 - INFO - Sending batch comparison request with prompt: Compare Banana and Grape and return the items in the correct order as 'item1,item2'.
2024-09-11 10:46:23,539 - INFO - Received response: Banana,Grape
2024-09-11 10:46:23,540 - INFO - Sending batch comparison request with prompt: Compare Apple and Banana and return the items in the correct order as 'item1,item2'.
Compare Orange and Grape and return the items in the correct order as 'item1,item2'.
2024-09-11 10:46:24,067 - INFO - Received response: Apple,Banana
Grape,Orange
Original list: ['Apple', 'Orange', 'Banana', 'Grape', 'Pineapple']
Sorted list: ['Apple', 'Banana', 'Orange', 'Grape', 'Pineapple']
```

## Example 3: Chain of Thought

Run the **chain of thought** example to see how AgentM solves problems using a step-by-step reasoning approach:

```bash
python examples/chain_of_thought_example.py
```

### Sample Output:
```bash
Question: What is the square root of 144?
Chain of Thought Reasoning: To find the square root of 144 step-by-step, follow these steps:

1. **Understanding Square Roots**: The square root of a number is a value that, when multiplied by itself, gives that number. For example, if x is the square root of y, then x * x = y.

2. **Identifying the Number**: In this case, we need to find the square root of 144.

3. **Finding Factors**: We'll look for a number that, when multiplied by itself, equals 144.
4. **Testing Numbers**:
- Let's start with smaller numbers:
- 12 * 12 = 144 (this is the answer)

5. **Conclusion**: The square root of 144 is 12.

Thus, the final answer is:

\[
\sqrt{144} = 12
\]
```
## Example 4: Binary Classification List
Run the **binary classification list** example to classify items as healthy or unhealthy:
```bash
python examples/binary_classify_list_example.py
```
### Sample Output:
```bash
Classifying item: Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Apple' as true or false.
Received response for item: Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Apple' as true or false. -> True
Classifying item: Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Chocolate' as true or false.
Received response for item: Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Chocolate' as true or false. -> Chocolate can be classified as unhealthy (false) if it contains high levels of sugar and fat, such as most commercially available milk chocolates. However, dark chocolate with a high cocoa content (70% or more) has health benefits and can be considered healthy in moderation.

Without specific context, the classification can vary. Generally:

- If we consider typical chocolate, it would be classified as unhealthy (false).
- If it is high-quality dark chocolate and consumed in moderation, it could be classified as healthy (true).

For general classification, I would classify 'Chocolate' as unhealthy (false).
Classifying item: Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Carrot' as true or false.
Received response for item: Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Carrot' as true or false. -> True
Original list: ['Apple', 'Chocolate', 'Carrot']
Binary classified results: [{'item': "Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Apple' as true or false.", 'classification': 'True'}, {'item': "Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Chocolate' as true or false.", 'classification': "Chocolate can be classified as unhealthy (false) if it contains high levels of sugar and fat, such as most commercially available milk chocolates. However, dark chocolate with a high cocoa content (70% or more) has health benefits and can be considered healthy in moderation. \n\nWithout specific context, the classification can vary. Generally:\n\n- If we consider typical chocolate, it would be classified as unhealthy (false).\n- If it is high-quality dark chocolate and consumed in moderation, it could be classified as healthy (true).\n\nFor general classification, I would classify 'Chocolate' as unhealthy (false)."}, {'item': "Based on the following criteria 'Classify each item as either healthy (true) or unhealthy (false)', classify the item 'Carrot' as true or false.", 'classification': 'True'}]
```
14 changes: 14 additions & 0 deletions examples/binary_classify_list_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import asyncio
from core.binary_classify_list_agent import BinaryClassifyListAgent

async def run_binary_classify_list_example():
items_to_classify = ['Apple', 'Chocolate', 'Carrot']
criteria = 'Classify each item as either healthy (true) or unhealthy (false)'
agent = BinaryClassifyListAgent(list_to_classify=items_to_classify, criteria=criteria)
classified_items = await agent.classify_list()

print("Original list:", items_to_classify)
print("Binary classified results:", classified_items)

if __name__ == "__main__":
asyncio.run(run_binary_classify_list_example())
36 changes: 36 additions & 0 deletions src/core/binary_classify_list_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import asyncio
from typing import List, Dict
from .openai_api import OpenAIClient
from .logging import Logger # Using correct logging abstraction

class BinaryClassifyListAgent:
def __init__(self, list_to_classify: List[str], criteria: str, max_tokens: int = 1000, temperature: float = 0.0):
self.list_to_classify = list_to_classify
self.criteria = criteria
self.max_tokens = max_tokens
self.temperature = temperature
self.openai_client = OpenAIClient()
self.logger = Logger()

async def classify_list(self) -> List[Dict]:
tasks = []
for item in self.list_to_classify:
user_prompt = f"Based on the following criteria '{self.criteria}', classify the item '{item}' as true or false."
tasks.append(self.classify_item(user_prompt))

results = await asyncio.gather(*tasks)
return results

async def classify_item(self, user_prompt: str) -> Dict:
system_prompt = "You are an assistant tasked with binary classification of items."

self.logger.info(f"Classifying item: {user_prompt}") # Logging the classification request

response = await self.openai_client.complete_chat([
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
], max_tokens=self.max_tokens)

self.logger.info(f"Received response for item: {user_prompt} -> {response.strip()}") # Logging the response

return {"item": user_prompt, "classification": response.strip()}

0 comments on commit 002b35a

Please sign in to comment.