-
Notifications
You must be signed in to change notification settings - Fork 0
/
anthropicProxy.js
143 lines (133 loc) · 3.4 KB
/
anthropicProxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const Anthropic = require("@anthropic-ai/sdk");
const fs = require("fs");
// Initialize Anthropic SDK
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
async function checkAnthropicAPI() {
// Check if the API can talk with Anthropic by making a simple request
const answer = await anthropic.completions.create({
model: "claude-2",
prompt: 'Human:say "pong" \nAssistant:',
max_tokens_to_sample: 100,
});
return answer.completion;
}
// Generate endpoint schema
async function generateEndpointSchema(prompt, filepath) {
const completion = await anthropic.completions.create({
model: "claude-2",
prompt: generatePrompt(prompt, filepath),
max_tokens_to_sample: 600,
});
// get the yaml from the completion
const regex = /<yaml>(.*?)<\/yaml>/s;
const match = completion.completion.match(regex);
return match ? match[1] : null;
}
// Generate prompt for endpoint schema
function generatePrompt(prompt, filepath) {
return `
Human: Write hello between <yaml></yaml>
Assistant: <yaml>hello</yaml>
Human: Please provide an OpenAPI 3.0 schema for this API endpoint between yaml:
<yaml>
${prompt}:
${fs.readFileSync(filepath, "utf8")}
</yaml>
Assistant:`;
}
// Validate spec
async function validateSpec(spec) {
const validYaml = await anthropic.completions.create({
model: "claude-2",
max_tokens_to_sample: 100000,
prompt: generateValidationPrompt(spec),
});
const regex = /<yaml>(.*?)<\/yaml>/s;
const match = validYaml.completion.match(regex);
return match ? match[1] : null;
}
// Generate validation prompt
function generateValidationPrompt(specYaml) {
return `
Human: Output valid OpenAPI yaml within <yaml></yaml> always:
Assistant:
<yaml>
openapi: 3.0.0
info:
title: Example API
description: An example to demonstrate OpenAPI 3.0
version: 1.0.0
servers:
- url: https://api.example.com
paths:
/users:
get:
summary: Gets a list of users
responses:
200:
description: Success
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
/users/{userId}:
get:
summary: Gets a user by ID
parameters:
- name: userId
in: path
required: true
schema:
type: integer
responses:
200:
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/User'
404:
description: User not found
put:
summary: Updates a user
parameters:
- name: userId
in: path
required: true
schema:
type: integer
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/UserUpdate'
responses:
200:
description: Success
404:
description: User not found
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
UserUpdate:
type: object
properties:
name:
type: string
</yaml>
Human: ${specYaml}
Output valid OpenAPI yaml within <yaml></yaml> fix errors and output a single yaml file:
Assistant:
`;
}
module.exports = { generateEndpointSchema, validateSpec, checkAnthropicAPI };