pcs-annotator 0.1.1__tar.gz → 0.1.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pcs-annotator
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: A Python library for classification tasks using multiple LLMs
5
5
  Project-URL: Homepage, https://github.com/salsina/PCS-Python-Library
6
6
  Project-URL: Issues, https://github.com/salsina/PCS-Python-Library/issues
@@ -2,8 +2,8 @@ from .LLM import LLM
2
2
  from .Tools import *
3
3
 
4
4
  class Annotator(LLM):
5
- def __init__(self, prompt, labels, model_name="llama3-8b-8192"):
6
- super().__init__(model_name)
5
+ def __init__(self, prompt, labels, model_name="llama3-8b-8192", token=None):
6
+ super().__init__(model_name, token)
7
7
 
8
8
  self.prompt = prompt
9
9
  self.labels = labels
@@ -0,0 +1,67 @@
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from langchain_groq import ChatGroq
4
+ from langchain_core.output_parsers import StrOutputParser
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain_anthropic import ChatAnthropic
7
+ # from langchain_huggingface import HuggingFaceEndpoint
8
+ from huggingface_hub import InferenceClient # Use InferenceClient instead of HuggingFaceEndpoint
9
+ from langchain_huggingface import HuggingFaceEndpoint
10
+
11
+ # from langchain_huggingface import HuggingFacePipeline
12
+
13
+
14
+ class LLM:
15
+ def __init__(self, model_name, token=None):
16
+ if token:
17
+ self.key = token
18
+ else:
19
+ load_dotenv()
20
+ self.model_name = model_name
21
+ self.llm = self._initialize_model()
22
+ parser = StrOutputParser()
23
+ self.llm = self.llm | parser
24
+
25
+ def _initialize_model(self):
26
+ """
27
+ Initialize the appropriate LLM model based on the model_name.
28
+ """
29
+ if self.model_name.startswith("llama"): # e.g., llama-3.1-8b-instant
30
+ if self.key == None:
31
+ self.key = os.getenv('GROQ_API_KEY')
32
+ if not self.key:
33
+ raise ValueError("Missing GROQ_API_KEY in environment variables.")
34
+ model = ChatGroq(api_key=self.key, model_name=self.model_name, temperature=0.1)
35
+
36
+ elif self.model_name.startswith("gpt"): # e.g., gpt-4
37
+ if self.key == None:
38
+ self.key = os.getenv('OPENAI_API_KEY')
39
+ if not self.key:
40
+ raise ValueError("Missing OPENAI_API_KEY in environment variables.")
41
+ model = ChatOpenAI(api_key=self.key, model=self.model_name, temperature=0.1)
42
+
43
+ elif self.model_name.startswith("claude"): # e.g., claude-3-5-sonnet-20241022
44
+ if self.key == None:
45
+ self.key = os.getenv('ANTHROPIC_API_KEY')
46
+ if not self.key:
47
+ raise ValueError("Missing ANTHROPIC_API_KEY in environment variables.")
48
+ model = ChatAnthropic(api_key=self.key, model=self.model_name, temperature=0.1)
49
+
50
+ elif self.model_name.startswith("mistral") or self.model_name.startswith("google"): # e.g., mistralai/Mistral-7B-Instruct-v0.3 or google/gemma-2-9b-it
51
+ if self.key == None:
52
+ self.key = os.getenv('HUGGINGFACE_API_KEY')
53
+ if not self.key:
54
+ raise ValueError("Missing HUGGINGFACE_API_KEY in environment variables.")
55
+
56
+ model = HuggingFaceEndpoint(
57
+ repo_id=self.model_name,
58
+ task="text-generation",
59
+ temperature=0.1,
60
+ huggingfacehub_api_token=self.key,
61
+ )
62
+
63
+
64
+ else:
65
+ raise ValueError(f"Unsupported model name: {self.model_name}")
66
+
67
+ return model
@@ -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"]):
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):
13
13
  self.prompt=prompt
14
14
  if os.path.exists(dataset_path):
15
15
  self.dataset_path = dataset_path
@@ -23,7 +23,16 @@ class PCS:
23
23
  self.dataset = pd.read_csv(dataset_path)
24
24
  self.possible_labels = self.dataset["label"].unique().tolist()
25
25
  for annotator_model_name in self.annotator_model_names:
26
- self.annotators.append(Annotator(prompt=self.prompt, labels=self.possible_labels, model_name=annotator_model_name))
26
+ if annotator_model_name.startswith("llama"):
27
+ token = GROQ_API_KEY
28
+ elif annotator_model_name.startswith("gpt"):
29
+ token = OPENAI_API_KEY
30
+ elif annotator_model_name.startswith("claude"):
31
+ token = ANTHROPIC_API_KEY
32
+ elif annotator_model_name.startswith("mistral") or annotator_model_name.startswith("google"):
33
+ token = HUGGINGFACE_API_KEY
34
+
35
+ self.annotators.append(Annotator(prompt=self.prompt, labels=self.possible_labels, model_name=annotator_model_name, token=token))
27
36
  self.optimal_llm_weights = [1] * len(self.annotators)
28
37
  self.optimal_mr_weights = [1] * 4
29
38
  self.optimal_label_thresholds = [0.5] * len(self.possible_labels)
@@ -2,11 +2,11 @@ from .LLM import LLM
2
2
  from .Tools import *
3
3
 
4
4
  class TextMutator(LLM):
5
- def __init__(self, model_name="llama-3.1-8b-instant"):
6
- super().__init__(model_name)
5
+ def __init__(self, model_name="llama-3.1-8b-instant", token=None):
6
+ super().__init__(model_name, token)
7
7
 
8
8
 
9
- def MutateText(self, text, mr, custom_prompt=None):
9
+ def MutateText(self, text, mr):
10
10
  if mr == "passive_active":
11
11
  prompt = f"""Review the text below and transform all sentences by converting active voice to passive voice and vice versa, where appropriate.
12
12
 
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
 
6
6
  [project]
7
7
  name = "pcs-annotator"
8
- version = "0.1.1"
8
+ version = "0.1.2"
9
9
  authors = [
10
10
  { name = "Sina Salimian", email = "sina99.sn@gmail.com" }
11
11
  ]
@@ -1,74 +0,0 @@
1
- import os
2
- from dotenv import load_dotenv
3
- from langchain_groq import ChatGroq
4
- from langchain_core.output_parsers import StrOutputParser
5
- from langchain_openai import ChatOpenAI
6
- from langchain_anthropic import ChatAnthropic
7
- # from langchain_huggingface import HuggingFaceEndpoint
8
- from huggingface_hub import InferenceClient # Use InferenceClient instead of HuggingFaceEndpoint
9
- from langchain_huggingface import HuggingFaceEndpoint
10
-
11
- # from langchain_huggingface import HuggingFacePipeline
12
-
13
-
14
- class LLM:
15
- def __init__(self, model_name):
16
- load_dotenv()
17
- self.model_name = model_name
18
- self.llm = self._initialize_model()
19
- parser = StrOutputParser()
20
- self.llm = self.llm | parser
21
-
22
- def _initialize_model(self):
23
- """
24
- Initialize the appropriate LLM model based on the model_name.
25
- """
26
- if self.model_name.startswith("llama"): # e.g., llama-3.1-8b-instant
27
- key = os.getenv('GROQ_API_KEY')
28
- if not key:
29
- raise ValueError("Missing GROQ_API_KEY in environment variables.")
30
- model = ChatGroq(api_key=key, model_name=self.model_name, temperature=0.1)
31
-
32
- elif self.model_name.startswith("gpt"): # e.g., gpt-4
33
- key = os.getenv('OPENAI_API_KEY')
34
- if not key:
35
- raise ValueError("Missing OPENAI_API_KEY in environment variables.")
36
- model = ChatOpenAI(api_key=key, model=self.model_name, temperature=0.1)
37
-
38
- elif self.model_name.startswith("claude"): # e.g., claude-3-5-sonnet-20241022
39
- key = os.getenv('ANTHROPIC_API_KEY')
40
- if not key:
41
- raise ValueError("Missing ANTHROPIC_API_KEY in environment variables.")
42
- model = ChatAnthropic(api_key=key, model=self.model_name, temperature=0.1)
43
-
44
- elif self.model_name.startswith("mistral"): # e.g., mistralai/Mistral-7B-Instruct-v0.3
45
- key = os.getenv('HUGGINGFACE_API_KEY')
46
-
47
- if not key:
48
- raise ValueError("Missing HUGGINGFACE_API_KEY in environment variables.")
49
-
50
- model = HuggingFaceEndpoint(
51
- repo_id="mistralai/Mistral-7B-Instruct-v0.3",
52
- task="text-generation",
53
- temperature=0.1,
54
- huggingfacehub_api_token=key,
55
- )
56
-
57
-
58
-
59
- elif self.model_name.startswith("google"): # e.g., gemma-2-9b-it
60
- key = os.getenv('HUGGINGFACE_API_KEY')
61
- if not key:
62
- raise ValueError("Missing HUGGINGFACE_API_KEY in environment variables.")
63
-
64
- model = HuggingFaceEndpoint(
65
- repo_id="google/gemma-2-9b-it",
66
- task="text-generation",
67
- temperature=0.1,
68
- huggingfacehub_api_token=key,
69
- )
70
-
71
- else:
72
- raise ValueError(f"Unsupported model name: {self.model_name}")
73
-
74
- return model
File without changes
File without changes