File size: 1,820 Bytes
7c31ea0
5009282
9b2def5
d1a9af7
5009282
 
9b2def5
 
5009282
9b2def5
5009282
9b2def5
5009282
9b2def5
5009282
9b2def5
 
 
5009282
9b2def5
 
 
5009282
9b2def5
 
 
 
5009282
 
 
9b2def5
5009282
 
 
 
9b2def5
5009282
 
 
 
9b2def5
 
 
 
 
5009282
 
 
 
 
 
 
 
9b2def5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from tools.base_tool import BaseTool
from mistral_hf_wrapper import MistralInference

class ClassifierTool(BaseTool):
    name = 'open_classifier'
    description = "Classifies given items into categories from a specific knowledge area."

    inputs = {
        'knowledge_area': {
            'type': 'string',
            'description': 'Domain or field for classification (e.g., biology, technology).',
        },
        'environment': {
            'type': 'string',
            'description': 'Brief description of the context in which items are evaluated.',
        },
        'categories': {
            'type': 'string',
            'description': 'Comma-separated list of categories to classify into.',
        },
        'items': {
            'type': 'string',
            'description': 'Comma-separated list of items to classify.',
        },
    }
    output_type = 'string'

    def __init__(self, model=None):
        self.model = model or MistralInference()
        super().__init__()

    def forward(self, knowledge_area: str, environment: str, categories: str, items: str) -> str:
        prompt = self._build_prompt(knowledge_area, environment, categories, items)
        response = self.model(prompt)
        return response.strip()

    def _build_prompt(self, knowledge_area, environment, categories, items) -> str:
        return f"""You are an expert classifier in the field of {knowledge_area}.
Context: {environment}
You must assign each item below to one of the following categories:
{categories}

Items to classify:
{items}

Rules:
- Use your {knowledge_area} knowledge only.
- Use the environment only if it's essential for disambiguation.
- Use "Other" only if an item clearly doesn't belong to any provided category.
- Format:
Category 1: item1, item2
Category 2: item3
Other: item4
"""