Categories
Software Development

Analyzing Cursor’s GPT-4 API Request

Here’s a how Cursor, a VSCode fork with built-in GPT-4 support, crafts its API requests to GPT-4 to generate code.

Original API Request

{
  "model": "gpt-4",
  "temperature": 0,
  "stop": [
    "\n// End Generation Here",
    "\n```"
  ],
  "messages": [
    {
      "role": "system",
      "content": "You are an intelligent programmer. You are helping a colleague insert a piece of code in a file.\n\nYour colleague is going to give you a file and an insertion point, along with a set of instructions. Please write code at the insertion point following their instructions.\n\nThink carefully and critically about the code to insert that best follows their instructions."
    },
    {
      "role": "user",
      "content": "This is my current file. The insertion point will be denoted by the comments: Start Generation Here and End Generation Here\n```sample3.php\n// Start Generation Here\n// End Generation Here\n\n```\n\n-------\n\n## Potentially helpful context\n\n#### file_context_0\n\nsample2.php from line 1:\n```\nHi <?php echo htmlspecialchars($\_POST['name']); ?>.\nYou are <?php echo (int)$\_POST['age']; ?> years old.\n\n```\n\n#### file_context_1\n\nsample.php from line 1:\n```\n<?php\\nif (str_contains($\_SERVER['HTTP_USER_AGENT'], 'Firefox')) {\\n echo 'You are using Firefox.';\\n}\\n?>\n\n```"
    },
    {
      "role": "user",
      "content": "## Instructions\n\n### Generation Prompt\n\ncreate some sample php code showing cool PHP features\n\n## Your Task\n\nGenerate the code to be inserted in accordance with the instructions.\n\nPlease format your output as:\n```\n// Start Generation Here\n// INSERT_YOUR_CODE\n// End Generation Here\n```"
    }
  ],
  "user": "auth0|65c48922e9e729b486601890",
  "stream": true
}

Analysis

  • Model: Using GPT-4.
  • Temperature: A value of 0 ensures consistent output.
  • Stop Sequences: They define where the AI’s output should conclude, probably important for integrating the generated code with existing code.
  • Messages:
    • The system message outlines the context.
    • First User message supplies the AI with the current file’s state, where to insert the code, and additional context from related files to understand the broader project environment.
    • The final user message contains specific instructions for what the AI is supposed to generate, including the format for the output.

Leave a Reply

Your email address will not be published. Required fields are marked *