pcs-annotator 0.1.2__tar.gz → 0.1.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
1
+ Metadata-Version: 2.4
2
+ Name: pcs-annotator
3
+ Version: 0.1.4
4
+ Summary: A Python library for classification tasks using multiple LLMs
5
+ Project-URL: Homepage, https://github.com/salsina/PCS-Python-Library
6
+ Project-URL: Issues, https://github.com/salsina/PCS-Python-Library/issues
7
+ Author-email: Sina Salimian <sina99.sn@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.8
13
+ Requires-Dist: langchain
14
+ Requires-Dist: langchain-anthropic
15
+ Requires-Dist: langchain-community
16
+ Requires-Dist: langchain-groq
17
+ Requires-Dist: langchain-huggingface
18
+ Requires-Dist: langchain-openai
19
+ Requires-Dist: python-dotenv
20
+ Description-Content-Type: text/markdown
21
+
22
+ # PCS Annotator: A Python Library for Classification Tasks Using Multiple LLMs
23
+
24
+ ## How to Install
25
+
26
+ ```bash
27
+ pip install pcs-annotator
28
+ ```
29
+
30
+ ## How to Use
31
+
32
+ ```python
33
+ from pcs_annotator import PCS
34
+ ```
35
+
36
+ ### Create a Prompt for Classification Tasks
37
+
38
+ ```python
39
+ prompt = """Analyze the news article and determine whether it is 'Fake' or 'Real.'
40
+ Label the article using the tag format: <label>Fake</label> or <label>Real</label>.
41
+ Afterward, provide your reasoning after the “<reasoning>” tag and close it with "</reasoning>".
42
+ Respond only with the label and the reasoning.
43
+ """
44
+ ```
45
+
46
+ ### Training Dataset Path (`dataset_path`)
47
+
48
+ You will need a CSV file with two columns: `label` and `text` to ensure better annotation, as the data is used to train the LLMs.
49
+
50
+
51
+
52
+
53
+ ## 🔧 Customizing Hyperparameters
54
+
55
+ The `PCS` class allows you to customize various hyperparameters, including the choice of annotator models, text mutator models, and API keys for different LLM providers.
56
+
57
+ ### 📌 Example Usage
58
+ ```python
59
+ pcs = PCS(
60
+ prompt="Your classification prompt here",
61
+ dataset_path="path/to/dataset.csv",
62
+ annotators=["llama3-8b-8192", "mistralai/Mistral-7B-Instruct-v0.3", "google/gemma-2-9b-it"],
63
+ textmutator="llama-3.1-8b-instant",
64
+ GROQ_API_KEY=None,
65
+ OPENAI_API_KEY=None,
66
+ ANTHROPIC_API_KEY=None,
67
+ HUGGINGFACE_API_KEY=None,
68
+ train=True
69
+ )
70
+
71
+ print(pcs.annotate("America is a country"))
72
+ ```
73
+
74
+ # Configuration
75
+
76
+ ## 🔹 Annotators (`annotators`)
77
+ Defines the list of LLMs used for annotation.
78
+ * **Default Models**:
79
+ * `"llama3-8b-8192"`
80
+ * `"mistralai/Mistral-7B-Instruct-v0.3"`
81
+ * `"google/gemma-2-9b-it"`
82
+ * **Customization**:
83
+ * You can add, remove, or modify the annotators by passing a list of model names.
84
+
85
+ ## 🔹 Text Mutator (`textmutator`)
86
+ Determines the LLM used for generating text mutations.
87
+ * **Default Model**: `"llama-3.1-8b-instant"`
88
+ * **Customization**:
89
+ * You can replace it by passing a different model name as a string.
90
+
91
+ ## 🔹 API Keys
92
+ To access certain LLM models, you need to provide API keys. These can be passed as arguments during initialization or set as environment variables in a `.env` file.
93
+
94
+ | API Key | Purpose |
95
+ |---------|---------|
96
+ | `GROQ_API_KEY` | Required for **Llama models**. Set via argument or `.env` file. |
97
+ | `OPENAI_API_KEY` | Required for **OpenAI models** (e.g., `"gpt-4"`). |
98
+ | `ANTHROPIC_API_KEY` | Required for **Anthropic models** (e.g., `"claude-3-5-sonnet-20241022"`). |
99
+ | `HUGGINGFACE_API_KEY` | Required for **Mistral and Google Gemma models**. |
100
+
101
+
102
+ ## 🔹 Continue the Training (`train`)
103
+ Determines whether to continue optimizing weights using more data or not
104
+ * **Default Value**: `True`
105
+ * **Customization**:
106
+ * You can change to `False`
@@ -0,0 +1,85 @@
1
+ # PCS Annotator: A Python Library for Classification Tasks Using Multiple LLMs
2
+
3
+ ## How to Install
4
+
5
+ ```bash
6
+ pip install pcs-annotator
7
+ ```
8
+
9
+ ## How to Use
10
+
11
+ ```python
12
+ from pcs_annotator import PCS
13
+ ```
14
+
15
+ ### Create a Prompt for Classification Tasks
16
+
17
+ ```python
18
+ prompt = """Analyze the news article and determine whether it is 'Fake' or 'Real.'
19
+ Label the article using the tag format: <label>Fake</label> or <label>Real</label>.
20
+ Afterward, provide your reasoning after the “<reasoning>” tag and close it with "</reasoning>".
21
+ Respond only with the label and the reasoning.
22
+ """
23
+ ```
24
+
25
+ ### Training Dataset Path (`dataset_path`)
26
+
27
+ You will need a CSV file with two columns: `label` and `text` to ensure better annotation, as the data is used to train the LLMs.
28
+
29
+
30
+
31
+
32
+ ## 🔧 Customizing Hyperparameters
33
+
34
+ The `PCS` class allows you to customize various hyperparameters, including the choice of annotator models, text mutator models, and API keys for different LLM providers.
35
+
36
+ ### 📌 Example Usage
37
+ ```python
38
+ pcs = PCS(
39
+ prompt="Your classification prompt here",
40
+ dataset_path="path/to/dataset.csv",
41
+ annotators=["llama3-8b-8192", "mistralai/Mistral-7B-Instruct-v0.3", "google/gemma-2-9b-it"],
42
+ textmutator="llama-3.1-8b-instant",
43
+ GROQ_API_KEY=None,
44
+ OPENAI_API_KEY=None,
45
+ ANTHROPIC_API_KEY=None,
46
+ HUGGINGFACE_API_KEY=None,
47
+ train=True
48
+ )
49
+
50
+ print(pcs.annotate("America is a country"))
51
+ ```
52
+
53
+ # Configuration
54
+
55
+ ## 🔹 Annotators (`annotators`)
56
+ Defines the list of LLMs used for annotation.
57
+ * **Default Models**:
58
+ * `"llama3-8b-8192"`
59
+ * `"mistralai/Mistral-7B-Instruct-v0.3"`
60
+ * `"google/gemma-2-9b-it"`
61
+ * **Customization**:
62
+ * You can add, remove, or modify the annotators by passing a list of model names.
63
+
64
+ ## 🔹 Text Mutator (`textmutator`)
65
+ Determines the LLM used for generating text mutations.
66
+ * **Default Model**: `"llama-3.1-8b-instant"`
67
+ * **Customization**:
68
+ * You can replace it by passing a different model name as a string.
69
+
70
+ ## 🔹 API Keys
71
+ To access certain LLM models, you need to provide API keys. These can be passed as arguments during initialization or set as environment variables in a `.env` file.
72
+
73
+ | API Key | Purpose |
74
+ |---------|---------|
75
+ | `GROQ_API_KEY` | Required for **Llama models**. Set via argument or `.env` file. |
76
+ | `OPENAI_API_KEY` | Required for **OpenAI models** (e.g., `"gpt-4"`). |
77
+ | `ANTHROPIC_API_KEY` | Required for **Anthropic models** (e.g., `"claude-3-5-sonnet-20241022"`). |
78
+ | `HUGGINGFACE_API_KEY` | Required for **Mistral and Google Gemma models**. |
79
+
80
+
81
+ ## 🔹 Continue the Training (`train`)
82
+ Determines whether to continue optimizing weights using more data or not
83
+ * **Default Value**: `True`
84
+ * **Customization**:
85
+ * You can change to `False`
@@ -13,10 +13,8 @@ from langchain_huggingface import HuggingFaceEndpoint
13
13
 
14
14
  class LLM:
15
15
  def __init__(self, model_name, token=None):
16
- if token:
17
- self.key = token
18
- else:
19
- load_dotenv()
16
+ self.key = token
17
+ load_dotenv()
20
18
  self.model_name = model_name
21
19
  self.llm = self._initialize_model()
22
20
  parser = StrOutputParser()
@@ -9,7 +9,7 @@ from tqdm import tqdm
9
9
 
10
10
 
11
11
  class PCS:
12
- def __init__(self, prompt, dataset_path, annotators=["llama3-8b-8192", "mistralai/Mistral-7B-Instruct-v0.3", "google/gemma-2-9b-it"], GROQ_API_KEY=None, OPENAI_API_KEY=None, ANTHROPIC_API_KEY=None, HUGGINGFACE_API_KEY=None):
12
+ def __init__(self, prompt, dataset_path, annotators=["llama3-8b-8192", "mistralai/Mistral-7B-Instruct-v0.3", "google/gemma-2-9b-it"], textmutator="llama-3.1-8b-instant", GROQ_API_KEY=None, OPENAI_API_KEY=None, ANTHROPIC_API_KEY=None, HUGGINGFACE_API_KEY=None, train=True):
13
13
  self.prompt=prompt
14
14
  if os.path.exists(dataset_path):
15
15
  self.dataset_path = dataset_path
@@ -17,7 +17,18 @@ class PCS:
17
17
  raise FileNotFoundError(f"Invalid dataset path {dataset_path}")
18
18
  self.annotator_model_names = annotators
19
19
  self.annotators = []
20
- self.textMutator = TextMutator()
20
+
21
+ if textmutator.startswith("llama"):
22
+ self.textMutator = TextMutator(model_name=textmutator, token=GROQ_API_KEY)
23
+ elif textmutator.startswith("gpt"):
24
+ self.textMutator = TextMutator(model_name=textmutator, token=OPENAI_API_KEY)
25
+ elif textmutator.startswith("claude"):
26
+ self.textMutator = TextMutator(model_name=textmutator, token=ANTHROPIC_API_KEY)
27
+ elif textmutator.startswith("mistral") or textmutator.startswith("google"):
28
+ self.textMutator = TextMutator(model_name=textmutator, token=HUGGINGFACE_API_KEY)
29
+ else:
30
+ raise ValueError(f"Unsupported text mutator model name: {textmutator}")
31
+
21
32
 
22
33
  self.annotations_filename = self.dataset_path.rsplit(".csv", 1)[0] + "_annotations.csv"
23
34
  self.dataset = pd.read_csv(dataset_path)
@@ -31,15 +42,18 @@ class PCS:
31
42
  token = ANTHROPIC_API_KEY
32
43
  elif annotator_model_name.startswith("mistral") or annotator_model_name.startswith("google"):
33
44
  token = HUGGINGFACE_API_KEY
45
+ else:
46
+ raise ValueError(f"Unsupported annotator model name: {annotator_model_name}")
34
47
 
35
48
  self.annotators.append(Annotator(prompt=self.prompt, labels=self.possible_labels, model_name=annotator_model_name, token=token))
36
49
  self.optimal_llm_weights = [1] * len(self.annotators)
37
50
  self.optimal_mr_weights = [1] * 4
38
51
  self.optimal_label_thresholds = [0.5] * len(self.possible_labels)
39
52
 
40
- print("Labelling the dataset...")
41
- self.create_annotations()
42
- print("Generated the dataset annotations.")
53
+ if train:
54
+ print("Labelling the dataset...")
55
+ self.create_annotations()
56
+ print("Generated the dataset annotations.")
43
57
 
44
58
  print("Optimizing weights...")
45
59
  self.PDE()
@@ -181,8 +195,10 @@ class PCS:
181
195
  return -accuracy # We want to maximize accuracy, so we minimize negative accuracy
182
196
 
183
197
  def PDE(self):
184
- df = pd.read_csv(self.annotations_filename)
185
-
198
+ if os.path.exists(self.annotations_filename):
199
+ df = pd.read_csv(self.annotations_filename)
200
+ else:
201
+ return
186
202
  # Define model column mappings
187
203
  model_columns = []
188
204
  for annotator in self.annotators:
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
 
6
6
  [project]
7
7
  name = "pcs-annotator"
8
- version = "0.1.2"
8
+ version = "0.1.4"
9
9
  authors = [
10
10
  { name = "Sina Salimian", email = "sina99.sn@gmail.com" }
11
11
  ]
@@ -1,24 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: pcs-annotator
3
- Version: 0.1.2
4
- Summary: A Python library for classification tasks using multiple LLMs
5
- Project-URL: Homepage, https://github.com/salsina/PCS-Python-Library
6
- Project-URL: Issues, https://github.com/salsina/PCS-Python-Library/issues
7
- Author-email: Sina Salimian <sina99.sn@gmail.com>
8
- License-Expression: MIT
9
- License-File: LICENSE
10
- Classifier: Operating System :: OS Independent
11
- Classifier: Programming Language :: Python :: 3
12
- Requires-Python: >=3.8
13
- Requires-Dist: langchain
14
- Requires-Dist: langchain-anthropic
15
- Requires-Dist: langchain-community
16
- Requires-Dist: langchain-groq
17
- Requires-Dist: langchain-huggingface
18
- Requires-Dist: langchain-openai
19
- Requires-Dist: python-dotenv
20
- Description-Content-Type: text/markdown
21
-
22
- This is a simple example package. You can use
23
- [GitHub-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
24
- to write your content.
@@ -1,3 +0,0 @@
1
- This is a simple example package. You can use
2
- [GitHub-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
3
- to write your content.
File without changes