Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug when resuming generation with OpenAI models #775

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions guidance/models/_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ def _generator(self, prompt, temperature):
assert (
role_name == "assistant"
), "Bad chat format! Last role before gen needs to be assistant!"
btext = prompt[pos:]
pos = len(prompt)
messages.append({"role": role_name, "content": btext.decode("utf8")})
break
btext = prompt[pos : pos + end_pos]
pos += end_pos + len(role_end)
Expand Down
18 changes: 18 additions & 0 deletions tests/models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,21 @@ def test_openai_chat_loop():

with assistant():
lm += gen(name="answer", max_tokens=2)


def test_openai_prefill():
try:
model = guidance.models.OpenAI("gpt-3.5-turbo", echo=False)
except:
pytest.skip("Skipping OpenAI test because we can't load the model!")

with system():
lm = model + "You are a helpful assistant."

with user():
lm += "What is the capital of France?"

with assistant():
lm += "The capital of France is " + gen(name="capital", suffix=".")

assert lm["capital"] == "Paris"
Loading