Skip to content

Commit 5b3fa1f

Browse files
committed
fix: concat logic and improve codecov
1 parent 24bf540 commit 5b3fa1f

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

prompt_string/string.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ def __add__(self, other):
7373
def __truediv__(self, other):
7474
from .string_chain import PromptChain
7575

76-
assert isinstance(other, PromptString)
77-
return PromptChain([self, other])
76+
if isinstance(other, PromptString):
77+
return PromptChain([self, other])
78+
elif isinstance(other, PromptChain):
79+
return PromptChain([self] + other.prompts)
80+
else:
81+
raise ValueError(f"Invalid type for Prompt Division: {type(other)}")
7882

7983
@to_prompt_string
8084
def replace(self, old, new, count=-1):

prompt_string/string_chain.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ def infer_roles(self):
4141
def roles(self):
4242
return [p.role for p in self.__prompts]
4343

44+
@property
45+
def prompts(self):
46+
return self.__prompts
47+
4448
def __truediv__(self, other):
4549
if isinstance(other, PromptChain):
4650
return PromptChain(
47-
self.__prompts + other.__prompts, default_start_role=self.__start_role
51+
self.prompts + other.prompts, default_start_role=self.__start_role
4852
)
4953
elif isinstance(other, PromptString):
5054
return PromptChain(
51-
self.__prompts + [other], default_start_role=self.__start_role
55+
self.prompts + [other], default_start_role=self.__start_role
5256
)
5357
else:
5458
raise ValueError(f"Invalid type for PromptChain Division: {type(other)}")

prompt_string/token.py

-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ def get_decoded_tokens(tokens: list[int]) -> str:
1212
return USE_ENCODER.decode(tokens)
1313

1414

15-
def truncate_string(content: str, max_tokens: int):
16-
return get_decoded_tokens(get_encoded_tokens(content)[:max_tokens])
17-
18-
1915
def setup_encoder(model: str = "gpt-4o"):
2016
global USE_ENCODER
2117
USE_ENCODER = encoding_for_model(model)

readme.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
<a href="https://pypi.org/project/prompt-string/">
77
<img src="https://img.shields.io/pypi/v/prompt-string.svg">
88
</a>
9+
<a href="https://github.com/memodb-io/prompt-string/actions/workflows/test.yml" >
10+
<img src="https://github.com/memodb-io/prompt-string/actions/workflows/test.yml/badge.svg"/>
11+
</a>
12+
<a href="https://codecov.io/github/memodb-io/prompt-string" >
13+
<img src="https://codecov.io/github/memodb-io/prompt-string/graph/badge.svg?token=kgeW8G0HYW"/>
14+
</a>
915
</div>
1016

1117

1218

19+
1320
Prompt is essentially a string, but it should behave somewhat differently from a standard string:
1421

1522
📏 **Length & Slicing**: A prompt string should consider the length in terms of tokens, not characters, and slicing should be done accordingly.
@@ -46,9 +53,9 @@ from prompt_string import P
4653

4754
prompt = P("you're a helpful assistant.")
4855

49-
print("Total token size", len(prompt))
50-
print("Decoded result of the second token", prompt[2])
51-
print("The decoded result of first five tokens", prompt[:5])
56+
print("Total token size:", len(prompt))
57+
print("Decoded result of the second token:", prompt[2])
58+
print("The decoded result of first three tokens:", prompt[:3])
5259
```
5360

5461
`P` supports some `str` native methods to still return a `P` object:
@@ -60,7 +67,7 @@ print("The decoded result of first five tokens", prompt[:5])
6067
prompt = P("you're a helpful assistant. {temp}")
6168

6269
print(len(prompt.format(temp="End of instructions")))
63-
print(len(prompt.replace("{temp}", ""))
70+
print(len(prompt.replace("{temp}", "")))
6471
```
6572

6673
> 🧐 Raise an issue if you think other methods should be supported
@@ -75,7 +82,7 @@ from prompt_string import P
7582
sp = P("you're a helpful assistant.", role="system")
7683
up = P("How are you?", role="user")
7784

78-
print(sp.role, up.role, (sp+up).roles)
85+
print(sp.role, up.role, (sp+up).role)
7986
print(sp + up)
8087

8188
print(sp.message())
@@ -84,7 +91,7 @@ print(sp.message())
8491
- role can be `None`, `str` for `P`
8592
- For single prompt, like `sp`, the role is `str`(*e.g.* `system`) or `None`
8693
- `sp+up` will concatenate two prompt string and generate a new `P`, whose role will be updated if the latter one has one.
87-
- For example, `sp+up`'s role is `user`, `sp+P('Hi')`'s role is `system`
94+
- For example, `sp+up`'s role is `user`; `sp+P('Hi')`'s role is `system`
8895

8996

9097
- `.message(...)` return a JSON object of this prompt.

tests/test_basic.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from prompt_string import P
23
from prompt_string.string_chain import PromptChain
34

@@ -11,6 +12,15 @@ def test_basic_role():
1112
assert sp.role == "user"
1213

1314

15+
def test_basic_index():
16+
p = P("you're a helpful assistant.")
17+
18+
assert str(p[0]) == "you're"
19+
assert str(p[1:]) == " a helpful assistant."
20+
with pytest.raises(ValueError):
21+
p[1.5]
22+
23+
1424
def test_basic_print():
1525
p = P("you're a helpful assistant.", role="user")
1626
print(p)
@@ -30,8 +40,11 @@ def test_basic_print():
3040
"content": "you're a helpful assistant.you're a helpful assistant.",
3141
}
3242

33-
p += p
34-
print(p, p.role)
43+
p4 = p + "Good"
44+
assert p4.message() == {
45+
"role": "user",
46+
"content": "you're a helpful assistant.Good",
47+
}
3548

3649

3750
def test_basic_chain():
@@ -42,6 +55,21 @@ def test_basic_chain():
4255
assert len(pc) == 3
4356
assert pc.infer_roles == ["user", "assistant", "user"]
4457
assert pc[2] is p
58+
assert len(pc[::2]) == 2
59+
60+
empty_pc = pc[100:]
61+
assert len(empty_pc) == 0
62+
assert empty_pc.infer_roles == []
63+
64+
pc_new = PromptChain([p, p2], default_start_role="system")
65+
assert pc_new.roles == [None, None]
66+
assert pc_new.infer_roles == ["system", "user"]
67+
68+
ppc = pc / pc
69+
assert len(ppc) == 6
70+
71+
pppc = p / pc
72+
assert len(pppc) == 4
4573

4674

4775
def test_basic_str_methods():

0 commit comments

Comments
 (0)