gpt-batch 0.1.6__py3-none-any.whl → 0.1.9__py3-none-any.whl

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.
gpt_batch/batcher.py CHANGED
@@ -1,57 +1,75 @@
1
1
  from openai import OpenAI
2
+ import anthropic
2
3
  from concurrent.futures import ThreadPoolExecutor, wait
3
4
  from functools import partial
4
5
  from tqdm import tqdm
6
+ import re
5
7
 
6
8
  class GPTBatcher:
7
9
  """
8
- A class to handle batching and sending requests to the OpenAI GPT model efficiently.
10
+ A class to handle batching and sending requests to the OpenAI GPT model and Anthropic Claude models efficiently.
9
11
 
10
12
  Attributes:
11
- client (OpenAI): The client instance to communicate with the OpenAI API using the provided API key.
12
- model_name (str): The name of the GPT model to be used. Default is 'gpt-3.5-turbo-0125'.
13
+ client: The client instance to communicate with the API (OpenAI or Anthropic).
14
+ is_claude (bool): Flag to indicate if using a Claude model.
15
+ model_name (str): The name of the model to be used. Default is 'gpt-3.5-turbo-0125'.
13
16
  system_prompt (str): Initial prompt or context to be used with the model. Default is an empty string.
14
17
  temperature (float): Controls the randomness of the model's responses. Higher values lead to more diverse outputs. Default is 1.
15
18
  num_workers (int): Number of worker threads used for handling concurrent requests. Default is 64.
16
19
  timeout_duration (int): Maximum time (in seconds) to wait for a response from the API before timing out. Default is 60 seconds.
17
20
  retry_attempts (int): Number of retries if a request fails. Default is 2.
18
21
  miss_index (list): Tracks the indices of requests that failed to process correctly.
19
-
20
- Parameters:
21
- api_key (str): API key for authenticating requests to the OpenAI API.
22
- model_name (str, optional): Specifies the GPT model version. Default is 'gpt-3.5-turbo-0125'.
23
- system_prompt (str, optional): Initial text or question to seed the model with. Default is empty.
24
- temperature (float, optional): Sets the creativity of the responses. Default is 1.
25
- num_workers (int, optional): Number of parallel workers for request handling. Default is 64.
26
- timeout_duration (int, optional): Timeout for API responses in seconds. Default is 60.
27
- retry_attempts (int, optional): How many times to retry a failed request. Default is 2.
28
22
  """
29
-
30
- def __init__(self, api_key, model_name="gpt-3.5-turbo-0125", system_prompt="",temperature=1,num_workers=64,timeout_duration=60,retry_attempts=2,api_base_url=None):
23
+ def __init__(self, api_key, model_name="gpt-3.5-turbo-0125", system_prompt="",temperature=1,num_workers=64,timeout_duration=60,retry_attempts=2,api_base_url=None,**kwargs):
24
+
25
+ self.is_claude = bool(re.search(r'claude', model_name, re.IGNORECASE))
26
+
27
+ if self.is_claude:
28
+ self.client = anthropic.Anthropic(api_key=api_key)
29
+ # Anthropic doesn't support custom base URL the same way
30
+ # If needed, this could be implemented differently
31
+ else:
32
+ self.client = OpenAI(api_key=api_key)
33
+ if api_base_url:
34
+ self.client.base_url = api_base_url
31
35
 
32
- self.client = OpenAI(api_key=api_key)
33
36
  self.model_name = model_name
34
37
  self.system_prompt = system_prompt
35
38
  self.temperature = temperature
36
39
  self.num_workers = num_workers
37
40
  self.timeout_duration = timeout_duration
38
41
  self.retry_attempts = retry_attempts
39
- self.miss_index =[]
40
- if api_base_url:
41
- self.client.base_url = api_base_url
42
+ self.miss_index = []
43
+ self.extra_params = kwargs
42
44
 
43
45
  def get_attitude(self, ask_text):
44
46
  index, ask_text = ask_text
45
47
  try:
46
- completion = self.client.chat.completions.create(
47
- model=self.model_name,
48
- messages=[
49
- {"role": "system", "content": self.system_prompt},
50
- {"role": "user", "content": ask_text}
51
- ],
52
- temperature=self.temperature,
53
- )
54
- return (index, completion.choices[0].message.content)
48
+ if self.is_claude:
49
+ # Use the Anthropic Claude API
50
+ message = self.client.messages.create(
51
+ model=self.model_name,
52
+ max_tokens=1024, # You can make this configurable if needed
53
+ messages=[
54
+ {"role": "user", "content": ask_text}
55
+ ],
56
+ system=self.system_prompt if self.system_prompt else None,
57
+ temperature=self.temperature,
58
+ **self.extra_params
59
+ )
60
+ return (index, message.content[0].text)
61
+ else:
62
+ # Use the OpenAI API as before
63
+ completion = self.client.chat.completions.create(
64
+ model=self.model_name,
65
+ messages=[
66
+ {"role": "system", "content": self.system_prompt},
67
+ {"role": "user", "content": ask_text}
68
+ ],
69
+ temperature=self.temperature,
70
+ **self.extra_params
71
+ )
72
+ return (index, completion.choices[0].message.content)
55
73
  except Exception as e:
56
74
  print(f"Error occurred: {e}")
57
75
  self.miss_index.append(index)
@@ -61,7 +79,7 @@ class GPTBatcher:
61
79
  new_list = []
62
80
  num_workers = self.num_workers
63
81
  timeout_duration = self.timeout_duration
64
- retry_attempts = 2
82
+ retry_attempts = self.retry_attempts
65
83
 
66
84
  executor = ThreadPoolExecutor(max_workers=num_workers)
67
85
  message_chunks = list(self.chunk_list(message_list, num_workers))
@@ -75,14 +93,14 @@ class GPTBatcher:
75
93
  new_list.extend(future.result() for future in done if future.done())
76
94
  if len(not_done) == 0:
77
95
  break
78
- future_to_message = {executor.submit(self.get_attitude, future_to_message[future]): future for future in not_done}
96
+ future_to_message = {executor.submit(self.get_attitude, future_to_message[future]): future_to_message[future] for future in not_done}
79
97
  except Exception as e:
80
98
  print(f"Error occurred: {e}")
81
99
  finally:
82
100
  executor.shutdown(wait=False)
83
101
  return new_list
84
102
 
85
- def complete_attitude_list(self,attitude_list, max_length):
103
+ def complete_attitude_list(self, attitude_list, max_length):
86
104
  completed_list = []
87
105
  current_index = 0
88
106
  for item in attitude_list:
@@ -106,7 +124,7 @@ class GPTBatcher:
106
124
  for i in range(0, len(lst), n):
107
125
  yield lst[i:i + n]
108
126
 
109
- def handle_message_list(self,message_list):
127
+ def handle_message_list(self, message_list):
110
128
  indexed_list = [(index, data) for index, data in enumerate(message_list)]
111
129
  max_length = len(indexed_list)
112
130
  attitude_list = self.process_attitude(indexed_list)
@@ -115,32 +133,50 @@ class GPTBatcher:
115
133
  attitude_list = [x[1] for x in attitude_list]
116
134
  return attitude_list
117
135
 
118
- def process_embedding(self,message_list):
119
- new_list = []
120
- executor = ThreadPoolExecutor(max_workers=self.num_workers)
121
- # Split message_list into chunks
122
- message_chunks = list(self.chunk_list(message_list, self.num_workers))
123
- fixed_get_embedding = partial(self.get_embedding)
124
- for chunk in tqdm(message_chunks, desc="Processing messages"):
125
- future_to_message = {executor.submit(fixed_get_embedding, message): message for message in chunk}
126
- for i in range(self.retry_attempts):
127
- done, not_done = wait(future_to_message.keys(), timeout=self.timeout_duration)
128
- for future in not_done:
129
- future.cancel()
130
- new_list.extend(future.result() for future in done if future.done())
131
- if len(not_done) == 0:
132
- break
133
- future_to_message = {executor.submit(fixed_get_embedding, future_to_message[future]): future_to_message[future] for future in not_done}
134
- executor.shutdown(wait=False)
135
- return new_list
136
- def get_embedding(self,text):
137
- index,text = text
138
- response = self.client.embeddings.create(
139
- input=text,
140
- model=self.model_name)
141
- return (index,response.data[0].embedding)
136
+ def get_embedding(self, text):
137
+ index, text = text
138
+ try:
139
+ if self.is_claude:
140
+ # Use Anthropic's embedding API if available
141
+ # Note: As of March 2025, make sure to check Anthropic's latest API
142
+ # for embeddings, as the format might have changed
143
+ response = self.client.embeddings.create(
144
+ model=self.model_name,
145
+ input=text
146
+ )
147
+ return (index, response.embedding)
148
+ else:
149
+ # Use OpenAI's embedding API
150
+ response = self.client.embeddings.create(
151
+ input=text,
152
+ model=self.model_name
153
+ )
154
+ return (index, response.data[0].embedding)
155
+ except Exception as e:
156
+ print(f"Error getting embedding: {e}")
157
+ self.miss_index.append(index)
158
+ return (index, None)
159
+
160
+ def process_embedding(self, message_list):
161
+ new_list = []
162
+ executor = ThreadPoolExecutor(max_workers=self.num_workers)
163
+ # Split message_list into chunks
164
+ message_chunks = list(self.chunk_list(message_list, self.num_workers))
165
+ fixed_get_embedding = partial(self.get_embedding)
166
+ for chunk in tqdm(message_chunks, desc="Processing messages"):
167
+ future_to_message = {executor.submit(fixed_get_embedding, message): message for message in chunk}
168
+ for i in range(self.retry_attempts):
169
+ done, not_done = wait(future_to_message.keys(), timeout=self.timeout_duration)
170
+ for future in not_done:
171
+ future.cancel()
172
+ new_list.extend(future.result() for future in done if future.done())
173
+ if len(not_done) == 0:
174
+ break
175
+ future_to_message = {executor.submit(fixed_get_embedding, future_to_message[future]): future_to_message[future] for future in not_done}
176
+ executor.shutdown(wait=False)
177
+ return new_list
142
178
 
143
- def handle_embedding_list(self,message_list):
179
+ def handle_embedding_list(self, message_list):
144
180
  indexed_list = [(index, data) for index, data in enumerate(message_list)]
145
181
  max_length = len(indexed_list)
146
182
  attitude_list = self.process_embedding(indexed_list)
@@ -152,5 +188,16 @@ class GPTBatcher:
152
188
  def get_miss_index(self):
153
189
  return self.miss_index
154
190
 
155
- # Add other necessary methods similar to the above, refactored to fit within this class structure.
156
-
191
+ # Example usage:
192
+ if __name__ == "__main__":
193
+ # For OpenAI
194
+ openai_batcher = GPTBatcher(
195
+ api_key="your_openai_api_key",
196
+ model_name="gpt-4-turbo"
197
+ )
198
+
199
+ # For Claude
200
+ claude_batcher = GPTBatcher(
201
+ api_key="your_anthropic_api_key",
202
+ model_name="claude-3-7-sonnet-20250219"
203
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gpt-batch
3
- Version: 0.1.6
3
+ Version: 0.1.9
4
4
  Summary: A package for batch processing with OpenAI API.
5
5
  Home-page: https://github.com/fengsxy/gpt_batch
6
6
  Author: Ted Yu
@@ -10,8 +10,8 @@ Platform: UNKNOWN
10
10
  Description-Content-Type: text/markdown
11
11
  Requires-Dist: openai
12
12
  Requires-Dist: tqdm
13
+ Requires-Dist: anthropic
13
14
 
14
- Certainly! Here's a clean and comprehensive README for your `GPTBatcher` tool, formatted in Markdown:
15
15
 
16
16
  ```markdown
17
17
  # GPT Batcher
@@ -62,6 +62,22 @@ print(result)
62
62
  # Expected output: ["embedding_1", "embedding_2", "embedding_3", "embedding_4"]
63
63
  ```
64
64
 
65
+ ### Handling Message Lists with different API
66
+
67
+ This example demonstrates how to send a list of questions and receive answers with different api:
68
+
69
+ ```python
70
+ from gpt_batch.batcher import GPTBatcher
71
+
72
+ # Initialize the batcher
73
+ batcher = GPTBatcher(api_key='sk-', model_name='deepseek-chat',api_base_url="https://api.deepseek.com/v1")
74
+
75
+
76
+ # Send a list of messages and receive answers
77
+ result = batcher.handle_message_list(['question_1', 'question_2', 'question_3', 'question_4'])
78
+
79
+ # Expected output: ["answer_1", "answer_2", "answer_3", "answer_4"]
80
+ ```
65
81
  ## Configuration
66
82
 
67
83
  The `GPTBatcher` class can be customized with several parameters to adjust its performance and behavior:
@@ -0,0 +1,8 @@
1
+ gpt_batch/__init__.py,sha256=zGDItktTxKLSQr44GY78dl5LKsSJig0Q59dzusqhU0U,59
2
+ gpt_batch/batcher.py,sha256=y8B4hIeQJQ16G5PvlNgHE-CtVQzHPhpBssOAg7npQLA,9083
3
+ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ tests/test_batcher.py,sha256=yRwqe2_VTp4wXXeQRhyDPZ2NZ-H3SSCDAxlNNXh3Aro,5314
5
+ gpt_batch-0.1.9.dist-info/METADATA,sha256=30t3VH_tY1mNWnzBPuQWKmD1o3bcA9yh3htvAWBgyok,3401
6
+ gpt_batch-0.1.9.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
7
+ gpt_batch-0.1.9.dist-info/top_level.txt,sha256=FtvJB_L9W_S6jL4G8Em_YWphG1wdKAF20BHUrf4B0yM,16
8
+ gpt_batch-0.1.9.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: bdist_wheel (0.45.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
tests/test_batcher.py CHANGED
@@ -18,6 +18,27 @@ def test_handle_message_list():
18
18
  assert len(results) == 2, "There should be two results, one for each message"
19
19
  assert all(len(result) >= 2 for result in results), "Each result should be at least two elements"
20
20
 
21
+
22
+ def test_json_format():
23
+ import json
24
+ # Initialize the GPTBatcher with hypothetical valid credentials
25
+ #api_key = #get from system environment
26
+ api_key = os.getenv('TEST_KEY')
27
+ if not api_key:
28
+ raise ValueError("API key must be set in the environment variables")
29
+ batcher = GPTBatcher(api_key=api_key, model_name='gpt-3.5-turbo-1106', system_prompt="Your task is to discuss privacy questions and provide persuasive answers with supporting reasons.",response_format={ "type": "json_object" })
30
+ message_list = ["return me a random json object", "return me a random json object"]
31
+
32
+ # Call the method under test
33
+ results = batcher.handle_message_list(message_list)
34
+ # Assertions to verify the length of the results and the structure of each item
35
+ assert len(results) == 2, "There should be two results, one for each message"
36
+ assert all(len(result) >= 2 for result in results), "Each result should be at least two elements"
37
+ #assert all(isinstance(result, dict) and 'json' in result for result in results), "Each result should be a JSON object with 'json' key"
38
+ assert all(isinstance(json.loads(result), dict) for result in results), "Each result should be a JSON object with 'json' key"
39
+
40
+
41
+
21
42
  def test_handle_embedding_list():
22
43
  # Initialize the GPTBatcher with hypothetical valid credentials
23
44
  #api_key = #get from system environment
@@ -51,5 +72,27 @@ def test_get_miss_index():
51
72
  miss_index = batcher.get_miss_index()
52
73
  assert miss_index == [], "The miss index should be empty"
53
74
  # Optionally, you can add a test configuration if you have specific needs
75
+
76
+
77
+ def test_claude_handle_message_list():
78
+ # Initialize the GPTBatcher with Claude model
79
+ api_key = os.getenv('ANTHROPIC_API_KEY')
80
+ if not api_key:
81
+ raise ValueError("Anthropic API key must be set in the environment variables as ANTHROPIC_API_KEY")
82
+
83
+ batcher = GPTBatcher(
84
+ api_key=api_key,
85
+ model_name='claude-3-7-sonnet-20250219',
86
+ system_prompt="Your task is to discuss privacy questions and provide persuasive answers with supporting reasons."
87
+ )
88
+ message_list = ["I think privacy is important", "I don't think privacy is important"]
89
+
90
+ # Call the method under test
91
+ results = batcher.handle_message_list(message_list)
92
+
93
+ # Assertions to verify the length of the results and the structure of each item
94
+ assert len(results) == 2, "There should be two results, one for each message"
95
+ assert all(isinstance(result, str) and len(result) > 0 for result in results if result is not None), "Each result should be a non-empty string if not None"
96
+ assert batcher.is_claude, "Should recognize model as Claude"
54
97
  if __name__ == "__main__":
55
98
  pytest.main()
@@ -1,8 +0,0 @@
1
- gpt_batch/__init__.py,sha256=zGDItktTxKLSQr44GY78dl5LKsSJig0Q59dzusqhU0U,59
2
- gpt_batch/batcher.py,sha256=jKLK-iuByg3Mc2ZungT5aZYzO60c5yO-YXCOf_70O6w,7591
3
- tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- tests/test_batcher.py,sha256=N88RZrSuBaDti6Lry7xipyGXHn3jKg85O12mjcHHZA0,3006
5
- gpt_batch-0.1.6.dist-info/METADATA,sha256=Q0EhkVe8YbKac3JjhASu3_wY3y9hV_YJqqwVEzlf9wc,2932
6
- gpt_batch-0.1.6.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
7
- gpt_batch-0.1.6.dist-info/top_level.txt,sha256=FtvJB_L9W_S6jL4G8Em_YWphG1wdKAF20BHUrf4B0yM,16
8
- gpt_batch-0.1.6.dist-info/RECORD,,