promptfoo

Форк
0
/
webhook.ts 
69 строк · 1.7 Кб
1
import logger from '../logger';
2
import { fetchWithCache } from '../cache';
3

4
import { REQUEST_TIMEOUT_MS } from './shared';
5

6
import type { ApiProvider, ProviderResponse } from '../types.js';
7

8
export class WebhookProvider implements ApiProvider {
9
  webhookUrl: string;
10
  config?: object;
11

12
  constructor(webhookUrl: string, options: { id?: string; config?: object } = {}) {
13
    const { id, config } = options;
14
    this.webhookUrl = webhookUrl;
15
    this.id = id ? () => id : this.id;
16
    this.config = config;
17
  }
18

19
  id(): string {
20
    return `webhook:${this.webhookUrl}`;
21
  }
22

23
  toString(): string {
24
    return `[Webhook Provider ${this.webhookUrl}]`;
25
  }
26

27
  async callApi(prompt: string): Promise<ProviderResponse> {
28
    const params: { prompt: string; config?: object } = {
29
      prompt,
30
    };
31
    if (this.config) {
32
      params.config = this.config;
33
    }
34

35
    logger.debug(`Calling Webhook: ${this.webhookUrl} with params: ${JSON.stringify(params)}`);
36
    let response;
37
    try {
38
      response = await fetchWithCache(
39
        this.webhookUrl,
40
        {
41
          method: 'POST',
42
          headers: {
43
            'Content-Type': 'application/json',
44
          },
45
          body: JSON.stringify(params),
46
        },
47
        REQUEST_TIMEOUT_MS,
48
        'json',
49
      );
50
    } catch (err) {
51
      return {
52
        error: `Webhook call error: ${String(err)}`,
53
      };
54
    }
55
    logger.debug(`\tWebhook response: ${JSON.stringify(response.data)}`);
56

57
    if (response.data && typeof response.data.output === 'string') {
58
      return {
59
        output: response.data.output,
60
      };
61
    } else {
62
      return {
63
        error: `Webhook response error: Unexpected response format: ${JSON.stringify(
64
          response.data,
65
        )}`,
66
      };
67
    }
68
  }
69
}
70

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.