Skip to content

Commit bb02b01

Browse files
CoveMBericglau
andauthored
Plat 6355 provide current generated contract as context to open ai (#505)
Co-authored-by: Eric Lau <[email protected]>
1 parent d478508 commit bb02b01

File tree

4 files changed

+219
-150
lines changed

4 files changed

+219
-150
lines changed

packages/ui/api/ai.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,46 @@ import {
1111
import { getRedisInstance } from './services/redis.ts';
1212
import { getOpenAiInstance } from './services/open-ai.ts';
1313
import { getEnvironmentVariableOr } from './utils/env.ts';
14+
import type { Chat } from '../src/types.ts';
15+
16+
type AiChatBodyRequest = {
17+
messages: Chat[];
18+
currentCode: string;
19+
currentOpts: Record<string, Record<string, string | boolean>>;
20+
chatId: string;
21+
};
22+
23+
const buildAiChatMessages = (request: AiChatBodyRequest): Chat[] => {
24+
const validatedMessages = request.messages.filter((message: { role: string; content: string }) => {
25+
return message.content.length < 500;
26+
});
27+
28+
return [
29+
{
30+
role: 'system',
31+
content: `
32+
You are a smart contract assistant built by OpenZeppelin to help users using OpenZeppelin Contracts Wizard.
33+
The current options are ${JSON.stringify(request.currentOpts)}.
34+
The current contract code is ${request.currentCode}
35+
Please be kind and concise. Keep responses to <100 words.
36+
`.trim(),
37+
},
38+
...validatedMessages,
39+
];
40+
};
1441

1542
export default async (req: Request): Promise<Response> => {
1643
try {
17-
const data = await req.json();
44+
const aiChatBodyRequest: AiChatBodyRequest = await req.json();
1845

1946
const redis = getRedisInstance();
2047
const openai = getOpenAiInstance();
2148

22-
const validatedMessages = data.messages.filter((message: { role: string; content: string }) => {
23-
return message.content.length < 500;
24-
});
25-
26-
const messages = [
27-
{
28-
role: 'system',
29-
content: `
30-
You are a smart contract assistant built by OpenZeppelin to help users using OpenZeppelin Contracts Wizard.
31-
The current options are ${JSON.stringify(data.currentOpts)}.
32-
Please be kind and concise. Keep responses to <100 words.
33-
`.trim(),
34-
},
35-
...validatedMessages,
36-
];
49+
const aiChatMessages = buildAiChatMessages(aiChatBodyRequest);
3750

3851
const response = await openai.chat.completions.create({
3952
model: getEnvironmentVariableOr('OPENAI_MODEL', 'gpt-4o-mini'),
40-
messages,
53+
messages: aiChatMessages,
4154
functions: [
4255
erc20Function,
4356
erc721Function,
@@ -53,13 +66,13 @@ export default async (req: Request): Promise<Response> => {
5366

5467
const stream = OpenAIStream(response, {
5568
async onCompletion(completion) {
56-
const id = data.chatId;
69+
const id = aiChatBodyRequest.chatId;
5770
const updatedAt = Date.now();
5871
const payload = {
5972
id,
6073
updatedAt,
6174
messages: [
62-
...messages,
75+
...aiChatMessages,
6376
{
6477
content: completion,
6578
role: 'assistant',

packages/ui/src/solidity/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
</script>
252252

253253
<div class="container flex flex-col gap-4 p-4 rounded-3xl">
254-
<Wiz bind:functionCall={functionCall} bind:currentOpts={opts}></Wiz>
254+
<Wiz bind:functionCall={functionCall} bind:currentOpts={opts} bind:currentCode={code}></Wiz>
255255

256256
<div class="header flex flex-row justify-between">
257257
<div class="tab overflow-hidden whitespace-nowrap">

0 commit comments

Comments
 (0)