Spaces:
Running
Running
Update services/geminiService.ts
Browse files- services/geminiService.ts +21 -13
services/geminiService.ts
CHANGED
|
@@ -39,6 +39,8 @@ const evaluationSchema = {
|
|
| 39 |
const graphResponseSchema = {
|
| 40 |
type: Type.OBJECT,
|
| 41 |
properties: {
|
|
|
|
|
|
|
| 42 |
reasoning: {
|
| 43 |
type: Type.STRING,
|
| 44 |
description: "Brief explanation of architectural choices or fixes applied."
|
|
@@ -591,6 +593,7 @@ export const applyPatches = (code: string, patches: { search: string, replace: s
|
|
| 591 |
*/
|
| 592 |
export const evaluateCode = async (
|
| 593 |
prompt: string,
|
|
|
|
| 594 |
code: string,
|
| 595 |
model: string = DEFAULT_MODEL
|
| 596 |
): Promise<{ score: number, missing_components: string[], bugs: string[], improvement_tasks: string[] }> => {
|
|
@@ -598,10 +601,10 @@ export const evaluateCode = async (
|
|
| 598 |
try {
|
| 599 |
const response = await ai.models.generateContent({
|
| 600 |
model: model,
|
| 601 |
-
contents: `Original Prompt: "${prompt}"\n\nGenerated Code:\n${code}`,
|
| 602 |
config: {
|
| 603 |
-
systemInstruction: `You are a Senior QA Engineer. Compare the generated PyTorch code against the original prompt.
|
| 604 |
-
Identify any missing requirements, logical bugs, or incomplete components.
|
| 605 |
Generate a list of targeted improvement tasks.
|
| 606 |
Return the evaluation in JSON format.`,
|
| 607 |
responseMimeType: "application/json",
|
|
@@ -656,14 +659,18 @@ Return the patches in JSON format.`,
|
|
| 656 |
* Uses the detailed prompt to write, review, and polish PyTorch code.
|
| 657 |
*/
|
| 658 |
export const generateCodeWithAgents = async (
|
|
|
|
|
|
|
| 659 |
promptText: string,
|
| 660 |
onStatusUpdate: (status: AgentStatus, log: string) => void,
|
| 661 |
model: string = DEFAULT_MODEL
|
| 662 |
): Promise<{ code: string, evaluation?: any, initialPatches?: any }> => {
|
| 663 |
const ai = getAiClient();
|
|
|
|
|
|
|
| 664 |
|
| 665 |
// --- Step 1: Coder ---
|
| 666 |
-
onStatusUpdate('architect', 'Coder Agent is writing initial PyTorch implementation...');
|
| 667 |
|
| 668 |
const coderSystemInstruction = `You are a Senior Deep Learning Engineer.
|
| 669 |
Your task is to write clean, modular, and production-ready PyTorch code.
|
|
@@ -673,11 +680,11 @@ Coding Standards:
|
|
| 673 |
2. Include docstrings for the class and forward method.
|
| 674 |
3. Add comments indicating the expected tensor shapes at each major step.
|
| 675 |
4. Use descriptive variable names.
|
| 676 |
-
5. Include a robust 'if __name__ == "__main__":' block that
|
| 677 |
6. Handle custom layer logic or external imports if specified.
|
| 678 |
7. Initialization Strategy:
|
| 679 |
- Use Kaiming initialization (nn.init.kaiming_normal_) for Conv2d and Linear layers.
|
| 680 |
-
- CRITICAL: If using SiLU (nn.SiLU) as the activation, use nonlinearity='linear' (gain=1)
|
| 681 |
|
| 682 |
Return ONLY the Python code. No markdown backticks.`;
|
| 683 |
|
|
@@ -685,13 +692,13 @@ Return ONLY the Python code. No markdown backticks.`;
|
|
| 685 |
try {
|
| 686 |
const response = await ai.models.generateContent({
|
| 687 |
model: model,
|
| 688 |
-
contents: `Request: "${promptText}"`,
|
| 689 |
config: {
|
| 690 |
systemInstruction: coderSystemInstruction,
|
| 691 |
tools: [{ googleSearch: {} }, { urlContext: {} }]
|
| 692 |
}
|
| 693 |
});
|
| 694 |
-
draftCode = response.text
|
| 695 |
} catch(e) {
|
| 696 |
throw e;
|
| 697 |
}
|
|
@@ -700,12 +707,13 @@ Return ONLY the Python code. No markdown backticks.`;
|
|
| 700 |
onStatusUpdate('critic', 'Reviewer Agent is analyzing code for bugs and optimization...');
|
| 701 |
|
| 702 |
const reviewerSystemInstruction = `You are a Senior Code Reviewer.
|
| 703 |
-
Analyze the provided PyTorch code
|
|
|
|
| 704 |
- Syntax errors or missing imports.
|
| 705 |
- Logical bugs (e.g., wrong dimension in cat/stack).
|
| 706 |
- Missing super().__init__() calls.
|
| 707 |
- Inefficient implementations.
|
| 708 |
-
- Adherence to the original architecture request.
|
| 709 |
You have access to Google Search to verify PyTorch documentation or SOTA implementation details.
|
| 710 |
|
| 711 |
Provide a concise, technical critique. If the code is perfect, say "No changes needed".`;
|
|
@@ -715,7 +723,7 @@ Provide a concise, technical critique. If the code is perfect, say "No changes n
|
|
| 715 |
const response = await ai.models.generateContent({
|
| 716 |
model: model,
|
| 717 |
contents: [
|
| 718 |
-
{ role: 'user', parts: [{ text: `
|
| 719 |
{ role: 'model', parts: [{ text: draftCode }] },
|
| 720 |
{ role: 'user', parts: [{ text: "Please review this code." }] }
|
| 721 |
],
|
|
@@ -747,7 +755,7 @@ Return ONLY the final Python code. No markdown backticks.`;
|
|
| 747 |
const response = await ai.models.generateContent({
|
| 748 |
model: model,
|
| 749 |
contents: [
|
| 750 |
-
{ role: 'user', parts: [{ text: `
|
| 751 |
{ role: 'model', parts: [{ text: draftCode }] },
|
| 752 |
{ role: 'user', parts: [{ text: `Critique: ${critique}\n\nApply the critique and provide the final polished code.` }] }
|
| 753 |
],
|
|
@@ -757,7 +765,7 @@ Return ONLY the final Python code. No markdown backticks.`;
|
|
| 757 |
|
| 758 |
// --- Step 4: Evaluator (New Step) ---
|
| 759 |
onStatusUpdate('evaluator', 'Evaluator Agent is verifying requirements...');
|
| 760 |
-
const evaluation = await evaluateCode(promptText, finalCode, model);
|
| 761 |
|
| 762 |
let initialPatches = null;
|
| 763 |
if (evaluation.score < 90 && evaluation.improvement_tasks.length > 0) {
|
|
|
|
| 39 |
const graphResponseSchema = {
|
| 40 |
type: Type.OBJECT,
|
| 41 |
properties: {
|
| 42 |
+
model_type: { type: Type.STRING, description: "The overarching type of the model (e.g., 'cnn_transformer_hybrid', 'vision_transformer')." },
|
| 43 |
+
input_shape: { type: Type.ARRAY, items: { type: Type.NUMBER }, description: "Input shape [C,H,W] or [N,C,H,W] or other." },
|
| 44 |
reasoning: {
|
| 45 |
type: Type.STRING,
|
| 46 |
description: "Brief explanation of architectural choices or fixes applied."
|
|
|
|
| 593 |
*/
|
| 594 |
export const evaluateCode = async (
|
| 595 |
prompt: string,
|
| 596 |
+
irRepresentation: string,
|
| 597 |
code: string,
|
| 598 |
model: string = DEFAULT_MODEL
|
| 599 |
): Promise<{ score: number, missing_components: string[], bugs: string[], improvement_tasks: string[] }> => {
|
|
|
|
| 601 |
try {
|
| 602 |
const response = await ai.models.generateContent({
|
| 603 |
model: model,
|
| 604 |
+
contents: `Original Prompt: "${prompt}"\n\nArchitecture IR:\n${irRepresentation}\n\nGenerated Code:\n${code}`,
|
| 605 |
config: {
|
| 606 |
+
systemInstruction: `You are a Senior QA Engineer. Compare the generated PyTorch code against the original prompt and the formal IR schema.
|
| 607 |
+
Identify any missing requirements, shape mismatches, logical bugs, or incomplete components.
|
| 608 |
Generate a list of targeted improvement tasks.
|
| 609 |
Return the evaluation in JSON format.`,
|
| 610 |
responseMimeType: "application/json",
|
|
|
|
| 659 |
* Uses the detailed prompt to write, review, and polish PyTorch code.
|
| 660 |
*/
|
| 661 |
export const generateCodeWithAgents = async (
|
| 662 |
+
nodes: Node[],
|
| 663 |
+
edges: Edge[],
|
| 664 |
promptText: string,
|
| 665 |
onStatusUpdate: (status: AgentStatus, log: string) => void,
|
| 666 |
model: string = DEFAULT_MODEL
|
| 667 |
): Promise<{ code: string, evaluation?: any, initialPatches?: any }> => {
|
| 668 |
const ai = getAiClient();
|
| 669 |
+
|
| 670 |
+
const irRepresentation = JSON.stringify({ nodes, edges });
|
| 671 |
|
| 672 |
// --- Step 1: Coder ---
|
| 673 |
+
onStatusUpdate('architect', 'Coder Agent is writing initial PyTorch implementation from IR...');
|
| 674 |
|
| 675 |
const coderSystemInstruction = `You are a Senior Deep Learning Engineer.
|
| 676 |
Your task is to write clean, modular, and production-ready PyTorch code.
|
|
|
|
| 680 |
2. Include docstrings for the class and forward method.
|
| 681 |
3. Add comments indicating the expected tensor shapes at each major step.
|
| 682 |
4. Use descriptive variable names.
|
| 683 |
+
5. Include a robust 'if __name__ == "__main__":' block that acts as a HARD VERIFIER. It must instantiate the model, run a dummy forward pass with torch.randn, print output shapes, calculate parameter counts, and print a 'Verification Passed!' message.
|
| 684 |
6. Handle custom layer logic or external imports if specified.
|
| 685 |
7. Initialization Strategy:
|
| 686 |
- Use Kaiming initialization (nn.init.kaiming_normal_) for Conv2d and Linear layers.
|
| 687 |
+
- CRITICAL: If using SiLU (nn.SiLU) as the activation, use nonlinearity='linear' (gain=1).
|
| 688 |
|
| 689 |
Return ONLY the Python code. No markdown backticks.`;
|
| 690 |
|
|
|
|
| 692 |
try {
|
| 693 |
const response = await ai.models.generateContent({
|
| 694 |
model: model,
|
| 695 |
+
contents: `Original Request: "${promptText}"\n\nArchitecture IR:\n${irRepresentation}`,
|
| 696 |
config: {
|
| 697 |
systemInstruction: coderSystemInstruction,
|
| 698 |
tools: [{ googleSearch: {} }, { urlContext: {} }]
|
| 699 |
}
|
| 700 |
});
|
| 701 |
+
draftCode = cleanJsonString(response.text);
|
| 702 |
} catch(e) {
|
| 703 |
throw e;
|
| 704 |
}
|
|
|
|
| 707 |
onStatusUpdate('critic', 'Reviewer Agent is analyzing code for bugs and optimization...');
|
| 708 |
|
| 709 |
const reviewerSystemInstruction = `You are a Senior Code Reviewer.
|
| 710 |
+
Analyze the provided PyTorch code against the Architecture IR constraints:
|
| 711 |
+
- Shape mismatch according to the IR
|
| 712 |
- Syntax errors or missing imports.
|
| 713 |
- Logical bugs (e.g., wrong dimension in cat/stack).
|
| 714 |
- Missing super().__init__() calls.
|
| 715 |
- Inefficient implementations.
|
| 716 |
+
- Adherence to the original architecture request and IR graph.
|
| 717 |
You have access to Google Search to verify PyTorch documentation or SOTA implementation details.
|
| 718 |
|
| 719 |
Provide a concise, technical critique. If the code is perfect, say "No changes needed".`;
|
|
|
|
| 723 |
const response = await ai.models.generateContent({
|
| 724 |
model: model,
|
| 725 |
contents: [
|
| 726 |
+
{ role: 'user', parts: [{ text: `Architecture IR:\n${irRepresentation}\n\nOriginal Request: "${promptText}"` }] },
|
| 727 |
{ role: 'model', parts: [{ text: draftCode }] },
|
| 728 |
{ role: 'user', parts: [{ text: "Please review this code." }] }
|
| 729 |
],
|
|
|
|
| 755 |
const response = await ai.models.generateContent({
|
| 756 |
model: model,
|
| 757 |
contents: [
|
| 758 |
+
{ role: 'user', parts: [{ text: `Architecture IR:\n${irRepresentation}\n\nOriginal Request: "${promptText}"` }] },
|
| 759 |
{ role: 'model', parts: [{ text: draftCode }] },
|
| 760 |
{ role: 'user', parts: [{ text: `Critique: ${critique}\n\nApply the critique and provide the final polished code.` }] }
|
| 761 |
],
|
|
|
|
| 765 |
|
| 766 |
// --- Step 4: Evaluator (New Step) ---
|
| 767 |
onStatusUpdate('evaluator', 'Evaluator Agent is verifying requirements...');
|
| 768 |
+
const evaluation = await evaluateCode(promptText, irRepresentation, finalCode, model);
|
| 769 |
|
| 770 |
let initialPatches = null;
|
| 771 |
if (evaluation.score < 90 && evaluation.improvement_tasks.length > 0) {
|