FD900 commited on
Commit
5009282
·
verified ·
1 Parent(s): f04d051

Update tools/classifier_tool.py

Browse files
Files changed (1) hide show
  1. tools/classifier_tool.py +30 -43
tools/classifier_tool.py CHANGED
@@ -1,67 +1,54 @@
1
- from smolagents import Tool
2
- import requests
3
- import os
4
 
5
- class ClassifyItemsTool(Tool):
6
- name = 'hf_classifier'
7
- description = """Categorizes a list of items using domain-specific knowledge. Intended for structured classification tasks."""
8
 
9
  inputs = {
10
- 'domain': {
11
  'type': 'string',
12
- 'description': 'The knowledge domain to use for classification (e.g., medicine, education, etc.).'
13
  },
14
- 'context': {
15
  'type': 'string',
16
- 'description': 'A brief description of the environment where items appear (helps disambiguate meanings).'
17
  },
18
  'categories': {
19
  'type': 'string',
20
- 'description': 'Comma-separated category list to classify items into.'
21
  },
22
  'items': {
23
  'type': 'string',
24
- 'description': 'Comma-separated list of items to classify.'
25
  },
26
  }
27
  output_type = 'string'
28
 
29
- def __init__(self, hf_model_url: str | None = None, **kwargs):
30
- self.api_url = hf_model_url or os.getenv("HF_ENDPOINT")
31
- if not self.api_url:
32
- raise ValueError("HF_ENDPOINT must be set as environment variable or passed to constructor.")
33
- super().__init__(**kwargs)
34
 
35
- def forward(self, domain: str, context: str, categories: str, items: str) -> str:
36
- prompt = self._build_prompt(domain, context, categories, items)
 
 
37
 
38
- response = requests.post(
39
- self.api_url,
40
- headers={"Content-Type": "application/json"},
41
- json={"inputs": prompt}
42
- )
43
- response.raise_for_status()
44
- result = response.json()
45
-
46
- if isinstance(result, list) and 'generated_text' in result[0]:
47
- return result[0]['generated_text'].strip()
48
- elif isinstance(result, dict) and 'generated_text' in result:
49
- return result['generated_text'].strip()
50
- return str(result)
51
-
52
- def _build_prompt(self, domain: str, context: str, categories: str, items: str) -> str:
53
- return f"""
54
- You are a {domain} expert working within a {context} context.
55
- Classify the following items into the specified categories using your domain expertise.
56
-
57
- Categories:
58
  {categories}
59
 
60
  Items to classify:
61
  {items}
62
 
63
- Return explanation followed by a classification in this format:
64
- Category 1: item list
65
- Category 2: item list
66
- Other (if uncertain): item list
 
 
 
 
67
  """
 
1
+ from tools.base import Tool
2
+ from mistral_hf_wrapper import MistralInference
 
3
 
4
+ class ClassifierTool(Tool):
5
+ name = 'open_classifier'
6
+ description = "Classifies given items into categories from a specific knowledge area."
7
 
8
  inputs = {
9
+ 'knowledge_area': {
10
  'type': 'string',
11
+ 'description': 'Domain or field for classification (e.g., biology, technology).',
12
  },
13
+ 'environment': {
14
  'type': 'string',
15
+ 'description': 'Brief description of the context in which items are evaluated.',
16
  },
17
  'categories': {
18
  'type': 'string',
19
+ 'description': 'Comma-separated list of categories to classify into.',
20
  },
21
  'items': {
22
  'type': 'string',
23
+ 'description': 'Comma-separated list of items to classify.',
24
  },
25
  }
26
  output_type = 'string'
27
 
28
+ def __init__(self, model=None):
29
+ self.model = model or MistralInference()
30
+ super().__init__()
 
 
31
 
32
+ def forward(self, knowledge_area: str, environment: str, categories: str, items: str) -> str:
33
+ prompt = self._build_prompt(knowledge_area, environment, categories, items)
34
+ response = self.model(prompt)
35
+ return response.strip()
36
 
37
+ def _build_prompt(self, knowledge_area, environment, categories, items) -> str:
38
+ return f"""You are an expert classifier in the field of {knowledge_area}.
39
+ Context: {environment}
40
+ You must assign each item below to one of the following categories:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  {categories}
42
 
43
  Items to classify:
44
  {items}
45
 
46
+ Rules:
47
+ - Use your {knowledge_area} knowledge only.
48
+ - Use the environment only if it's essential for disambiguation.
49
+ - Use "Other" only if an item clearly doesn't belong to any provided category.
50
+ - Format:
51
+ Category 1: item1, item2
52
+ Category 2: item3
53
+ Other: item4
54
  """