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
// This val receives text input, sends it to OpenAI to generate relationships,
// and returns a newline-delimited list of relationships.
// It uses the OpenAI API to generate the relationships.
// Tradeoff: This approach relies on an external API, which may have rate limits or costs.
// Usage with curl:
// curl -X POST -H "Content-Type: text/plain" -d "Your text here" https://your-val-url.web.val.run
import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
export default async function main(req: Request): Promise<Response> {
if (req.method !== "POST") {
return new Response("Method not allowed. Use POST with text in the body.", { status: 405 });
}
const text = await req.text();
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "Given a chunk of content, create a list of relationships in this specific format: [entity1_type:entity1_name] -[:RELATIONSHIP_TYPE]-> [entity2_type:entity2_name]. Output each relationship on a new line."
},
{ role: "user", content: text }
],
temperature: 0.7,
max_tokens: 200,
});
const relationships = completion.choices[0].message.content.trim();
return new Response(relationships, {
headers: { "Content-Type": "text/plain" }
});
}