Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

现在怎样做代码补全工具和代码解释工具?

查看完整题解

参考回答

直接回答

代码补全优先用 FIM,即给光标左右的上下文,让模型补中间缺口。解释代码更适合普通聊天接口,给必要的函数、调用上下文和问题。

讲清原理

旧 Coder 的名称属于相应发布时期。使用当前在线 FIM 时,按现行官方文档选模型、接口和非思考模式。IDE 可以按文件权限收集相关片段,再运行格式检查和测试。

例子与易错点

例如光标位于函数中间时,给出函数签名和已经确定的后缀。下面用 SDK 向内存模拟服务发送 FIM 请求,验证地址和两段文本。解释报错时,提供脱敏后的最小调用与错误消息。

import json

import httpx
from openai import OpenAI

def fake_fim(request):
    body = json.loads(request.content)
    assert request.url.path == "/beta/completions"
    assert body["prompt"] == "def twice(number):\n"
    assert body["suffix"] == "\n\n# next function\n"
    return httpx.Response(200, json={"id": "local-fim", "object": "text_completion",
        "created": 0, "model": "deepseek-flash",
        "choices": [{"index": 0, "text": "    return 2 * number", "finish_reason": "stop"}]})

prefix = "def twice(number):\n"
suffix = "\n\n# next function\n"
with OpenAI(api_key="local-test-key", base_url="https://api.deepseek.com/beta",
            http_client=httpx.Client(transport=httpx.MockTransport(fake_fim))) as client:
    response = client.completions.create(model="deepseek-flash", prompt=prefix,
                                         suffix=suffix, max_tokens=40)
source = prefix + response.choices[0].text + suffix
compile(source, "toy.py", "exec")  # Check syntax; do not execute model output.
print("FIM request kept prefix and suffix, and the assembled fixture compiles")

相关题:第 33 题、第 39 题、第 41 题、第 42 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。