LLMApi: AIP背景:保持整个Mock AIP呼声的一致性 (中文 (Chinese Simplified))

LLMApi: AIP背景:保持整个Mock AIP呼声的一致性

Wednesday, 05 November 2025

//

9 minute read

注:THs的文章主要是作为我作为释放文件的核素包的一部分产生的人工智能。

很有趣,所以我把它放在这里 但如果这是你的问题 请别管它

一. 导言 导言 导言 导言 导言 导言 一,导言 导言 导言 导言 导言 导言

**在建立和测试申请时,传统模拟API的最大挑战之一是其无国籍性质。**每个请求都返回完全随机的数据, 与先前的调用没有关系 。

如果您获取一个有 ID 123 的用户, 然后获取他们的订单, 没有保证命令会引用相同的用户 ID 。 AIPI 背景

给您的模拟 API 留下一个记忆来解决这个问题 。N Nuget 元数N Nuget 元数

你可以找到

吉特Hub在这里

sequenceDiagram
    participant Client
    participant MockAPI
    participant LLM

    Client->>MockAPI: GET /users/1
    MockAPI->>LLM: Generate user data
    LLM-->>MockAPI: {"id": 42, "name": "Alice"}
    MockAPI-->>Client: User data

    Client->>MockAPI: GET /orders?userId=42
    MockAPI->>LLM: Generate order data
    LLM-->>MockAPI: {"userId": 99, ...}
    MockAPI-->>Client: Order data (userId mismatch!)

对于项目,所有公共领域等等...

问题:无国籍混乱

传统的模拟API为每项请求独立生成数据:

sequenceDiagram
    participant Client
    participant MockAPI
    participant Context as Context Manager
    participant LLM

    Client->>MockAPI: GET /users/1?context=session-1
    MockAPI->>Context: Get history for "session-1"
    Context-->>MockAPI: (empty - first call)
    MockAPI->>LLM: Generate user (no context)
    LLM-->>MockAPI: {"id": 42, "name": "Alice"}
    MockAPI->>Context: Store: GET /users/1 → {"id": 42, ...}
    MockAPI-->>Client: User data

    Client->>MockAPI: GET /orders?context=session-1
    MockAPI->>Context: Get history for "session-1"
    Context-->>MockAPI: Previous call: user with id=42, name=Alice
    MockAPI->>LLM: Generate order (with context history)
    LLM-->>MockAPI: {"userId": 42, "customerName": "Alice", ...}
    MockAPI->>Context: Store: GET /orders → {"userId": 42, ...}
    MockAPI-->>Client: Order data (consistent!)

发现问题了吗?

用户有42号身份证,但订单是99号用户的。

相关电话之间没有一致性

解决方案:背景记忆

graph TD
    A[HTTP Request] --> B[ContextExtractor]
    B --> C{Context Name?}
    C -->|Yes| D[OpenApiContextManager]
    C -->|No| E[Generate without context]
    D --> F[Retrieve Context History]
    F --> G[PromptBuilder]
    E --> G
    G --> H[LLM]
    H --> I[Response]
    I --> J{Context Name?}
    J -->|Yes| K[Store in Context]
    J -->|No| L[Return Response]
    K --> L

**在 " API背景 " 中,模拟API在相关请求中保持了共同背景:**现在,LLM看到先前的用户呼叫,并生成引用相同用户身份和名称的订单。 **数据形成一个连贯的故事。**如何运作 建筑结构结构上下文系统由三个主要部分组成:

1. 目标 1. 目标

外 景 景 景 景 景 景 景 景 景 景 景 色ConcurrentDictionary:

public class OpenApiContextManager
{
    private readonly ConcurrentDictionary<string, ApiContext> _contexts;
    private const int MaxRecentCalls = 15;
    private const int SummarizeThreshold = 20;

    public void AddToContext(
        string contextName,
        string method,
        string path,
        string? requestBody,
        string responseBody)
    {
        var context = _contexts.GetOrAdd(contextName, _ => new ApiContext
        {
            Name = contextName,
            CreatedAt = DateTimeOffset.UtcNow,
            RecentCalls = new List<RequestSummary>(),
            SharedData = new Dictionary<string, string>(),
            TotalCalls = 0
        });

        context.RecentCalls.Add(new RequestSummary
        {
            Timestamp = DateTimeOffset.UtcNow,
            Method = method,
            Path = path,
            RequestBody = requestBody,
            ResponseBody = responseBody
        });

        ExtractSharedData(context, responseBody);

        if (context.RecentCalls.Count > MaxRecentCalls)
        {
            SummarizeOldCalls(context);
        }
    }
}

- 从请求中提取上下文名

  1. 目标
graph LR
    A[20+ Calls] --> B[Keep 15 Most Recent]
    A --> C[Summarize Older Calls]
    B --> D[Full Request/Response]
    C --> E[Summary: 'GET /users - called 5 times']
    D --> F[Included in LLM Prompt]
    E --> F
private void SummarizeOldCalls(ApiContext context)
{
    var toSummarize = context.RecentCalls
        .Take(context.RecentCalls.Count - MaxRecentCalls)
        .ToList();

    var summary = new StringBuilder();
    summary.AppendLine($"Earlier calls ({toSummarize.Count}):");

    var groupedByPath = toSummarize
        .GroupBy(c => $"{c.Method} {c.Path.Split('?')[0]}");

    foreach (var group in groupedByPath)
    {
        summary.AppendLine($"  {group.Key} - called {group.Count()} time(s)");
    }

    context.ContextSummary = summary.ToString();
    context.RecentCalls.RemoveRange(0, toSummarize.Count);
}

OpenApicon Text 管理器

  • 管理上下文储存和检索
private void ExtractSharedData(ApiContext context, string responseBody)
{
    using var doc = JsonDocument.Parse(responseBody);
    var root = doc.RootElement;

    if (root.ValueKind == JsonValueKind.Array && root.GetArrayLength() > 0)
    {
        var firstItem = root[0];
        ExtractValueIfExists(context, firstItem, "id", "lastId");
        ExtractValueIfExists(context, firstItem, "userId", "lastUserId");
        ExtractValueIfExists(context, firstItem, "name", "lastName");
        ExtractValueIfExists(context, firstItem, "email", "lastEmail");
    }
    else if (root.ValueKind == JsonValueKind.Object)
    {
        ExtractValueIfExists(context, root, "id", "lastId");
        ExtractValueIfExists(context, root, "userId", "lastUserId");
        ExtractValueIfExists(context, root, "name", "lastName");
        // ... more common patterns
    }
}

3 个

即时建筑

- 以LLLM提示方式包括上下文历史

内装储存

上下文使用线索安全程序存储在记忆中自动自动汇总

GET /api/mock/users?context=my-session
GET /api/mock/users?api-context=my-session

为了防止环境无限期增长和超过LLM象征性限制,当计数超过15时,该系统自动对旧电话进行总结:

GET /api/mock/users
X-Api-Context: my-session

共享数据提取

POST /api/mock/orders
Content-Type: application/json

{
  "context": "my-session",
  "shape": {"orderId": 0, "userId": 0}
}

上下文管理器自动从回复中提取通用标识符,以便于查阅:

这使该系统能够跟踪最新的用户身份、命令身份等,在上下文历史中提供。使用上下文具体说明背景的三种方式

您可以以三种不同的方式通过上下文名称,此优先顺序如下:

GET /api/mock/users/123?context=session-1

1. 目标 1. 目标

GET /api/mock/stream/stock-prices?context=trading-session
Accept: text/event-stream

查询参数

POST /graphql?context=my-app
Content-Type: application/json

{
  "query": "{ users { id name } }"
}

(最优先优先)

{
  "mostlylucid.mockllmapi": {
    "HubContexts": [
      {
        "Name": "stock-ticker",
        "Description": "Real-time stock prices",
        "ApiContextName": "stocks-session",
        "Shape": "{\"symbol\":\"string\",\"price\":0}"
      }
    ]
  }
}

2. 目标

HTTP 页眉

3 个

### 1. Create user
POST /api/mock/users?context=checkout-flow
{
  "shape": {
    "userId": 0,
    "name": "string",
    "email": "string",
    "address": {"street": "string", "city": "string"}
  }
}

### Response: {"userId": 42, "name": "Alice", ...}

### 2. Create cart (will reference same user)
POST /api/mock/cart?context=checkout-flow
{
  "shape": {
    "cartId": 0,
    "userId": 0,
    "items": [{"productId": 0, "quantity": 0}]
  }
}

### Response: {"cartId": 123, "userId": 42, ...}

### 3. Create order (consistent user and cart)
POST /api/mock/orders?context=checkout-flow
{
  "shape": {
    "orderId": 0,
    "userId": 0,
    "cartId": 0,
    "total": 0
  }
}

### Response: {"orderId": 789, "userId": 42, "cartId": 123, ...}

请求机构

支持的端点类型

### First call - establishes baseline
GET /api/mock/stocks?context=market-data
    &shape={"symbol":"string","price":0,"volume":0}

### Response: {"symbol": "ACME", "price": 145.50, "volume": 10000}

### Second call - price changes realistically
GET /api/mock/stocks?context=market-data
    &shape={"symbol":"string","price":0,"volume":0}

### Response: {"symbol": "ACME", "price": 146.20, "volume": 12000}
### Notice: Same symbol, price increased by $0.70 (realistic)

### Third call - continues the trend
GET /api/mock/stocks?context=market-data
    &shape={"symbol":"string","price":0,"volume":0}

### Response: {"symbol": "ACME", "price": 145.80, "volume": 11500}
### Notice: Price fluctuates but stays in realistic range

二、背景情况在

全部

终点类型 :

### Start game
POST /api/mock/game/start?context=game-session-123
{
  "shape": {
    "playerId": 0,
    "level": 0,
    "health": 0,
    "score": 0,
    "inventory": []
  }
}

### Response: {"playerId": 42, "level": 1, "health": 100, "score": 0}

### Complete quest
POST /api/mock/game/quest?context=game-session-123
{
  "shape": {
    "playerId": 0,
    "level": 0,
    "score": 0,
    "reward": {"item": "string", "value": 0}
  }
}

### Response: {"playerId": 42, "level": 2, "score": 500,
###           "reward": {"item": "Sword", "value": 100}}
### Notice: Same player, level increased, score increased

### Get stats
GET /api/mock/game/player?context=game-session-123
    &shape={"playerId":0,"level":0,"health":0,"score":0}

### Response: {"playerId": 42, "level": 2, "health": 100, "score": 500}
### Notice: Consistent with quest completion

REST APP APP REST APP

流通APPS

GET /api/openapi/contexts

### Response:
{
  "contexts": [
    {
      "name": "session-1",
      "totalCalls": 5,
      "recentCallCount": 5,
      "sharedDataCount": 3,
      "createdAt": "2025-01-15T10:00:00Z",
      "lastUsedAt": "2025-01-15T10:05:00Z",
      "hasSummary": false
    }
  ],
  "count": 1
}

图QL

GET /api/openapi/contexts/session-1

### Response shows full context including:
### - All recent calls with timestamps
### - Extracted shared data (IDs, names, emails)
### - Summary of older calls (if any)

信号( 通过配置)

DELETE /api/openapi/contexts/session-1

实际世界使用案例

DELETE /api/openapi/contexts

使用案例1:电子商务流动

以一致的用户和订单数据模拟完整的购物经验:

使用案例2:股票价格模拟

public async Task<string> HandleRequestAsync(
    string method,
    string fullPathWithQuery,
    string? body,
    HttpRequest request,
    HttpContext context,
    CancellationToken cancellationToken = default)
{
    // 1. Extract context name from request
    var contextName = _contextExtractor.ExtractContextName(request, body);

    // 2. Get context history if context specified
    var contextHistory = !string.IsNullOrWhiteSpace(contextName)
        ? _contextManager.GetContextForPrompt(contextName)
        : null;

    // 3. Build prompt with context history
    var prompt = _promptBuilder.BuildPrompt(
        method, fullPathWithQuery, body, shapeInfo,
        streaming: false, contextHistory: contextHistory);

    // 4. Get response from LLM
    var response = await _llmClient.GetCompletionAsync(prompt, cancellationToken);

    // 5. Store in context if context name provided
    if (!string.IsNullOrWhiteSpace(contextName))
    {
        _contextManager.AddToContext(
            contextName, method, fullPathWithQuery, body, response);
    }

    return response;
}

产生现实的股票价格波动,而不是随机值:

没有上下文,每通电话都会返回一个完全随机的符号和价格。

TASK: Generate a varied mock API response.
RULES: Output ONLY valid JSON. No markdown, no comments.

API Context: session-1
Total calls in session: 3

Shared data to maintain consistency:
  lastId: 42
  lastName: Alice
  lastEmail: [email protected]

Recent API calls:
  [10:00:05] GET /users/42
    Response: {"id": 42, "name": "Alice", "email": "[email protected]"}
  [10:00:12] GET /orders?userId=42
    Response: {"orderId": 123, "userId": 42, "items": [...]}

Generate a response that maintains consistency with the above context.

Method: POST
Path: /shipping/123
Body: {"orderId": 123}

在这种背景下,LLM保持同样的存货,并现实地调整价格。

使用案例3:游戏状态进步

通过游戏会话跟踪播放器进度 :

 Bad:  ?context=test1
 Good: ?context=user-checkout-flow-jan15

API 内地管理

列出所有上下文

### After completing your test scenario
DELETE /api/openapi/contexts/user-checkout-flow-jan15

获取上下文细节

清除特定上下文

GET /api/mock/users?context=demo-session
GET /api/mock/orders?context=demo-session
GET /api/mock/shipping?context=demo-session

清除全部上下文

实施细节

GET /api/openapi/contexts/demo-session

请求处理器整合

每个请求处理器(REST、流、图QL、信号R)都遵循同样的模式:

LLM 提示语背景

### Load spec with context
POST /api/openapi/specs
{
  "name": "petstore",
  "source": "https://petstore3.swagger.io/api/v3/openapi.json",
  "basePath": "/petstore",
  "contextName": "petstore-session"
}

### All petstore endpoints will share the same context

当存在上下文时,其历史包含在LLM提示中:

专卖局长会看到以前的所有电话,并会提出答复,其中提到相同的身份证、姓名和其他数据,并保持一致性。

最佳做法最佳做法

    1. 目标 1. 目标
  • 使用描述性上下文名
    1. 目标

完成时清除上下文内存的上下文持续到明确清除或服务器重新启动:

3 个

OpenApiContextManager相关端点之间的共享背景ConcurrentDictionary所有相关调用都使用相同的上下文名称 :

4. 4个。

监视上下文大小

检查上下文细节以查看存储多少个电话 :

  1. **如果您有许多电话( > 100),请考虑清空并重新开始,以避免时间过长的问题。**5 个
  2. 与 OpenAPI 光谱结合对于最大限度的现实主义, 请使用 OpenAPI 规格的背景 :
  3. 绩效考量内存使用
  4. **每种上下文仓库:**最多15个最近的电话(全面请求/答复)

旧电话汇总(压缩)

提取共享数据(小字典)

每个上下文的典型内存

:~ 50- 200 KB 取决于响应大小

Finding related posts...
logo

© 2026 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.