Skip to content

Commit f25eca4

Browse files
committed
[0.5.8] chore + Fix Gemini API body bug
1 parent 48e6531 commit f25eca4

File tree

7 files changed

+64
-20
lines changed

7 files changed

+64
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ docs/_build
160160
docs/_templates
161161
docs/source
162162
test.py
163+
wikigen/

docs/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ minor versions.
77

88
All relevant steps to be taken will be mentioned here.
99

10+
0.5.8
11+
-----
12+
13+
- If you have ``numpy`` installed in your environment, then ``tuneapi.utils.randomness.reservoir_sampling`` will honour
14+
the seed value. If you do not have ``numpy`` installed, then the seed value will be ignored.
15+
- Fix Bug in Gemini API body for functions with no parameters.
16+
1017
0.5.7
1118
-----
1219

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
project = "tuneapi"
1414
copyright = "2024, Frello Technologies"
1515
author = "Frello Technologies"
16-
release = "0.5.7"
16+
release = "0.5.8"
1717

1818
# -- General configuration ---------------------------------------------------
1919
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "tuneapi"
3-
version = "0.5.7"
3+
version = "0.5.8"
44
description = "Tune AI APIs."
55
authors = ["Frello Technology Private Limited <[email protected]>"]
66
license = "MIT"

tuneapi/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# Copyright © 2023- Frello Technology Private Limited
2+
3+
from tuneapi import apis as ta
4+
from tuneapi import endpoints as te
5+
from tuneapi import types as tt
6+
from tuneapi import utils as tu

tuneapi/apis/model_gemini.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515

1616
class Gemini(tt.ModelInterface):
17+
1718
def __init__(
1819
self,
19-
id: Optional[str] = "gemini-1.5-pro-latest",
20+
id: Optional[str] = "gemini-1.5-flash",
2021
base_url: str = "https://generativelanguage.googleapis.com/v1beta/models/{id}:{rpc}",
2122
extra_headers: Optional[Dict[str, str]] = None,
2223
):
@@ -120,21 +121,28 @@ def chat(
120121
**kwargs,
121122
) -> Any:
122123
output = ""
123-
for x in self.stream_chat(
124-
chats=chats,
125-
model=model,
126-
max_tokens=max_tokens,
127-
temperature=temperature,
128-
token=token,
129-
timeout=timeout,
130-
extra_headers=extra_headers,
131-
raw=False,
132-
**kwargs,
133-
):
134-
if isinstance(x, dict):
135-
output = x
124+
x = None
125+
try:
126+
for x in self.stream_chat(
127+
chats=chats,
128+
model=model,
129+
max_tokens=max_tokens,
130+
temperature=temperature,
131+
token=token,
132+
timeout=timeout,
133+
extra_headers=extra_headers,
134+
raw=False,
135+
**kwargs,
136+
):
137+
if isinstance(x, dict):
138+
output = x
139+
else:
140+
output += x
141+
except Exception as e:
142+
if not x:
143+
raise e
136144
else:
137-
output += x
145+
raise ValueError(x)
138146
return output
139147

140148
def stream_chat(
@@ -194,7 +202,14 @@ def stream_chat(
194202
"mode": "ANY",
195203
}
196204
}
197-
data["tools"] = [{"function_declarations": tools}]
205+
std_tools = []
206+
for i, t in enumerate(tools):
207+
props = t["parameters"]["properties"]
208+
t_copy = t.copy()
209+
if not props:
210+
t_copy.pop("parameters")
211+
std_tools.append(t_copy)
212+
data["tools"] = [{"function_declarations": std_tools}]
198213
data.update(kwargs)
199214

200215
if debug:

tuneapi/utils/randomness.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from threading import Lock
77
from snowflake import SnowflakeGenerator
88

9+
from tuneapi.utils.logger import logger
10+
911

1012
def get_random_string(length: int, numbers: bool = True, special: bool = False) -> str:
1113
choice_items = string.ascii_letters
@@ -39,14 +41,28 @@ def __call__(self, as_int=False) -> int:
3941
get_snowflake = SFGen()
4042

4143

42-
def reservoir_sampling(stream, k):
44+
def reservoir_sampling(stream, k, seed: int = 4):
4345
"""
4446
Perform reservoir sampling on the given stream to select k items.
4547
4648
:param stream: An iterable representing the input stream.
4749
:param k: The number of items to sample.
50+
:param seed: The seed to use for random number generation. Only used if numpy is available. If -1 is passed, the seed
51+
will be randomly generated.
4852
:return: A list containing k sampled items.
4953
"""
54+
try:
55+
import numpy as np
56+
57+
if seed == -1:
58+
seed = None
59+
rng = np.random.default_rng(seed)
60+
foo = rng.integers
61+
except ImportError:
62+
logger.warning(
63+
"Numpy not found, using python's random module. Seed will be ignored."
64+
)
65+
foo = random.randint
5066

5167
# Initialize an empty reservoir
5268
reservoir = []
@@ -57,7 +73,7 @@ def reservoir_sampling(stream, k):
5773
reservoir.append(item)
5874
else:
5975
# Randomly replace elements in the reservoir with decreasing probability
60-
j = random.randint(0, i)
76+
j = foo(0, i)
6177
if j < k:
6278
reservoir[j] = item
6379

0 commit comments

Comments
 (0)