Skip to content

Commit d7e29ce

Browse files
authored
fix(openai): include prior assistant responses in later requests (#102)
1 parent 014c428 commit d7e29ce

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/git_draft/bots/openai.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import json
1717
import logging
1818
from pathlib import PurePosixPath
19-
from typing import Any, Self, TypedDict, override
19+
from typing import Any, Self, TypedDict, cast, override
2020

2121
import openai
2222

2323
from ..common import JSONObject, UnreachableError, config_string, reindent
24+
from ..instructions import SYSTEM_PROMPT
2425
from .common import ActionSummary, Bot, Goal, UserFeedback, Worktree
2526

2627

@@ -159,19 +160,6 @@ def params(self) -> Sequence[openai.types.chat.ChatCompletionToolParam]:
159160
]
160161

161162

162-
# https://aider.chat/docs/more-info.html
163-
# https://github.com/Aider-AI/aider/blob/main/aider/prompts.py
164-
_INSTRUCTIONS = """
165-
You are an expert software engineer, who writes correct and concise code.
166-
Use the provided functions to find the files you need to answer the query,
167-
read the content of the relevant ones, and save the changes you suggest.
168-
169-
You should stop when and ONLY WHEN all the files you need to change have
170-
been updated. If you do not have enough information to complete your task,
171-
use the provided tool to request it from the user, then stop.
172-
"""
173-
174-
175163
class _ToolHandler[V]:
176164
def __init__(self, tree: Worktree, feedback: UserFeedback) -> None:
177165
self._tree = tree
@@ -241,7 +229,7 @@ async def act(
241229
tool_handler = _CompletionsToolHandler(tree, feedback)
242230

243231
messages: list[openai.types.chat.ChatCompletionMessageParam] = [
244-
{"role": "system", "content": reindent(_INSTRUCTIONS)},
232+
{"role": "system", "content": SYSTEM_PROMPT},
245233
{"role": "user", "content": goal.prompt},
246234
]
247235

@@ -254,10 +242,12 @@ async def act(
254242
tool_choice="required",
255243
)
256244
assert len(response.choices) == 1
245+
choice = response.choices[0]
257246
request_count += 1
258247

259248
done = True
260-
calls = response.choices[0].message.tool_calls
249+
messages.append(cast(Any, choice.message.to_dict(mode="json")))
250+
calls = choice.message.tool_calls
261251
for call in calls or []:
262252
output = tool_handler.handle_function(call.function)
263253
if output is not None:
@@ -302,7 +292,7 @@ def __init__(self, client: openai.OpenAI, model: str) -> None:
302292
def _load_assistant_id(self) -> str:
303293
kwargs: JSONObject = dict(
304294
model=self._model,
305-
instructions=reindent(_INSTRUCTIONS),
295+
instructions=SYSTEM_PROMPT,
306296
tools=_ToolsFactory(strict=True).params(),
307297
)
308298
path = self.state_folder_path(ensure_exists=True) / "ASSISTANT_ID"

src/git_draft/instructions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Shared assistant prompts"""
2+
3+
from .common import reindent
4+
5+
6+
# https://aider.chat/docs/more-info.html
7+
# https://github.com/Aider-AI/aider/blob/main/aider/prompts.py
8+
SYSTEM_PROMPT = reindent("""
9+
You are an expert software engineer, who writes correct and concise code.
10+
Use the provided functions to find the files you need to answer the query,
11+
read the content of the relevant ones, and save the changes you suggest.
12+
13+
You should stop when and ONLY WHEN all the files you need to change have
14+
been updated. If you do not have enough information to complete your task,
15+
use the provided tool to request it from the user, then stop.
16+
""")
17+
18+
19+
OFFLINE_ANSWER = reindent("""
20+
I'm unable to provide feedback at this time. Perform any changes you can
21+
and await further instructions. Do not request ask me any more questions
22+
until explicitly authorized to do so. Instead, add TODO comments in the
23+
code where relevant.
24+
""")

src/git_draft/progress.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import yaspin.core
1010

1111
from .bots import UserFeedback
12-
from .common import reindent, tagged
12+
from .common import tagged
1313
from .events import EventConsumer, feedback_events
14+
from .instructions import OFFLINE_ANSWER
1415

1516

1617
class Progress:
@@ -75,22 +76,16 @@ def ask(self, question: str) -> str:
7576
answer = self._ask(question)
7677
if answer is None:
7778
self.pending_question = question
78-
answer = _offline_answer
79+
answer = OFFLINE_ANSWER
7980
self._event_consumer.on_event(
8081
feedback_events.ReceiveUserGuidance(answer)
8182
)
82-
return _offline_answer
83+
return answer
8384

8485
def _ask(self, question: str) -> str | None:
8586
raise NotImplementedError()
8687

8788

88-
_offline_answer = reindent("""
89-
I'm unable to provide feedback at this time. Perform any final changes and
90-
await further instructions.
91-
""")
92-
93-
9489
class _DynamicProgress(Progress):
9590
def __init__(self) -> None:
9691
self._spinner: _DynamicProgressSpinner | None = None
@@ -191,4 +186,4 @@ def _notify(self, update: str) -> None:
191186
@override
192187
def _ask(self, question: str) -> str | None:
193188
self._progress.report(f"Feedback requested: {question}")
194-
return _offline_answer
189+
return OFFLINE_ANSWER

0 commit comments

Comments
 (0)