Skip to content

Commit

Permalink
Avoid losing code when prompting AI
Browse files Browse the repository at this point in the history
  • Loading branch information
bhackett1024 committed Feb 13, 2025
1 parent dd18d53 commit 6bbd93a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 52 deletions.
37 changes: 31 additions & 6 deletions app/lib/.server/llm/chat-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@ function convertContentToAnthropic(content: any): ContentBlockParam[] {
return [];
}

function flatMessageContent(content: string | ContentBlockParam[]): string {
if (typeof content === "string") {
return content;
}
if (Array.isArray(content)) {
let result = "";
for (const elem of content) {
if (elem.type === "text") {
result += elem.text;
}
}
return result;
}
console.log("AnthropicUnknownContent", JSON.stringify(content, null, 2));
return "AnthropicUnknownContent";
}

export async function chatAnthropic(chatController: ChatStreamController, apiKey: string, systemPrompt: string, messages: CoreMessage[]) {
const anthropic = new Anthropic({ apiKey });

const messageParams: MessageParam[] = [];

messageParams.push({
role: "assistant",
content: systemPrompt,
});

for (const message of messages) {
const role = message.role == "user" ? "user" : "assistant";
const content = convertContentToAnthropic(message.content);
Expand All @@ -38,16 +50,29 @@ export async function chatAnthropic(chatController: ChatStreamController, apiKey
});
}

console.log("AnthropicMessages", JSON.stringify(messageParams, null, 2));
console.log("************************************************");
console.log("AnthropicMessageSend");
console.log("Message system:");
console.log(systemPrompt);
for (const message of messageParams) {
console.log(`Message ${message.role}:`);
console.log(flatMessageContent(message.content));
}
console.log("************************************************");

const response = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
messages: messageParams,
max_tokens: MaxMessageTokens,
system: systemPrompt,
});

for (const content of response.content) {
if (content.type === "text") {
console.log("************************************************");
console.log("AnthropicMessageResponse:");
console.log(content.text);
console.log("************************************************");
chatController.writeText(content.text);
} else {
console.log("AnthropicUnknownResponse", JSON.stringify(content, null, 2));
Expand Down
46 changes: 0 additions & 46 deletions app/lib/common/prompts/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,52 +337,6 @@ Here are some examples of correct usage of artifacts:
</assistant_response>
</example>
</examples>
ULTRA IMPORTANT: When asked to fix a bug, the results of console logs from the bug are available as comments in the source, in the format of "// Repro:Name console.log(xyz) == 123".
Use these comments to get a better understanding of the bug and how data flows through the code, and describe what is causing the bug before starting on any code changes. You must fix the bug.
Here are examples of how to use these comments:
<examples>
<example>
<user_query>The Summation component does not render its result correctly. Fix this bug.</user_query>
<code_state>
function computeSum(a, b) {
return a + b;
// Repro:computeSum console.log(rval) == NaN, console.log(a) == 3, console.log(b) == undefined
}
function Summation(props) {
const a = props.a;
const b = props.b;
const rval = computeSum(a, b);
return <div>{rval}</div>; // Repro:Summation console.log(rval) == NaN
}
function createSummation(a) {
return <Summation a={3} b={undefined} />; // Repro:createSummation undefined value used by computeSum.
}
function main() {
createSummation(3); // Repro:main 3 value used by computeSum.
}
</code_state>
<assistant_response>
1. From Repro:Summation I can see that the NaN value was produced at Repro:computeSum.
2. From Repro:computeSum I can see that a is 3 and b is undefined. The latter is incorrect, it was produced at Repro:createSummation.
I will fix this by not passing undefined to the Summation component in createSummation.
<boltAction type="file" filePath="components/Summation.js">
... code changes to fix bug ...
</boltAction>
</assistant_response>
</example>
</examples>
ULTRA IMPORTANT: NEVER add any logging. It is your responsibility to use the Repro console log comments to understand the bug. The user is not going to do this for you.
`;

export const CONTINUE_PROMPT = stripIndents`
Expand Down

0 comments on commit 6bbd93a

Please sign in to comment.