一起来看看吗。
Couchbase N1QL,又名SQL++,是一种类似SQL的语言,用于查询存储在Couchbase中的JSON数据。
Couchbase N1QL中的CURL()函数可向外部服务发出HTTP请求,因而可用于通过向ChatGPT的API端点发送请求来调用ChatGPT。
Couchbase中的JavaScript UDF是用JavaScript编写的函数,不但可以作为N1QL查询的部分执行,还可用于通过向ChatGPT的API端点发出HTTP请求来调用ChatGPT。
要连接ChatGPT与Couchbase N1QL,我们需要知道ChatGPT的API端点以及访问它所需的身份验证凭据。
使用CURL()函数从Couchbase N1QL调用ChatGPT的语法如下所示:
SELECT CURL("https://api.openai.com/v1/engines/davinci/completions", {
"request": "POST",
"headers": [
"Authorization: Bearer ChangeToYourKeyElseWontWork",
"Content-Type: application/json"
],
"data": '{
"prompt": "Write a song on SQL using the style of Taylor Swift songs.",
"max_tokens": 200
}'
}) AS result ;
响应如下:
[
{
"result": {
"choices": [
{
"finish_reason": "length",
"index": 0,
"logprobs": null,
"text": " Where Swift sneakily commands, “…here you go / I wrote it on my phone so you could Google it,” you should write: “…here’s my query / I’ll drink my coffee and then will spool it.” Or, “…Here’s what I’m thinking / I pilfered my colleague’s project.” Only you can aspire to provide the official Taylor Swift SQL spoof song (don’t forget to play some chord tinkling).\n\n4. Invent an official buzzword for lambda expressions. Start a campaign to make “lambda-eme” or variants thereof the standard. Try to get lambda-me in all the common tools available (lamellar, lambmaster, lambator, lambda café, lambananas, and so on).\n\n5. Write about a process that took you too long in just 5 minutes. Make fun of"
}
],
"created": 1675103061,
"id": "cmpl-6eT7FnvGFN9HViONQnDhv5uabM6AO",
"model": "davinci",
"object": "text_completion",
"usage": {
"completion_tokens": 200,
"prompt_tokens": 13,
"total_tokens": 213
}
}
}
]
要将参数传递给ChatGPT,我们可以修改上述示例中的数据字段。如:
"data": '{
"prompt": "Write a song on SQL using the style of Taylor Swift songs.",
"temperature": 0.5,
"max_tokens": 200
}'
接下来我再具体展开讲讲Couchbase N1QL和CURL()。
Couchbase N1QL
Couchbase N1QL(Non-First Normal Form Query Language)是一种类似SQL的语言,用于查询存储在Couchbase中的JSON数据。它提供了用于转换和操作数据的各种函数,包括CURL()函数。
CURL()函数
CURL()函数允许我们从N1QL查询向外部服务发出HTTP请求。这对于集成Couchbase与其他系统(如web API)或获取web数据以便在Couchbase中进一步处理都非常有用。
在 Couchbase N1QL中使用CURL()函数的语法如下:
SELECT CURL(, );
SELECT RAW list FROM CURL("https://api.github.com/users/sitaramv/repos") AS list LIMIT 1;
CURL()函数向URL发送GET请求,请求返回用户sitaramv 的存储库。
函数的结果存储在响应变量list中,之后可使用其他N1QL函数进一步处理。
下面是如何使用CURL()函数将数据发布到外部的示例
select imagetext
from curl("https://vision.googleapis.com/v1/images:annotate?key=PUT YOUR KEY HERE",
{"request": "POST",
"header":"Content-Type: application/json",
"data": '{ "requests": [ { "image": { "source": { "imageUri": "https://www.528045.com/file/upload/202411/29/ulwc0ebxwnr.png" } }, "features": [ { "type": "TEXT_DETECTION" } ] } ] }'}) AS imagetext
在此示例中,CURL()函数向URL发送POST请求,请求主体中包含JSON有效负载。
自定义头Content-Type: application/json设置请求的数据为JSON格式。
Couchbase N1QL的CURL()函数提供了一种将Couchbase连接到外部系统并从web获取数据的便捷方法。由于能够指定HTTP方法、头和请求体,因而具有很大的灵活性,可用于各种方案。
Couchbase N1QL的JavaScript UDF允许我们编写自定义逻辑,然后可将其作为N1QL查询部分执行。这样我们就可以通过编写JavaScript代码来执行使用内置N1QL函数无法实现的复杂操作,非常便利。
JavaScript UDF
以下是如何在Couchbase N1QL中使用JavaScript UDF的示例:
curl -v -X POST \
http://localhost:8093/evaluator/v1/libraries/mysamples \
-u Administrator:password \
-H 'content-type: application/json' \
-d 'function square(val) { \
return val * val; \
}
CREATE FUNCTION square(val)
LANGUAGE JAVASCRIPT AS "square" AT "mysamples";
SELECT square(5) as result;
示例中创建了一个名为square的JavaScript UDF,该UDF接受单个参数val并返回其平方值。
square函数是使用CREATE FUNCTION语句定义的,该函数的JavaScript代码包含在一对$$中。LANGUAGE javascript选项指定函数的代码用JavaScript编写。
JavaScript UDF可以通过调用函数并传递必要的参数从而在N1QL查询中使用。此示例调用square函数并传入参数值5,并将结果存储在result变量中。
JavaScript UDF本身可以发出N1QL语句来对数据进行操作。
curl -v -X POST http://localhost:8093/evaluator/v1/libraries/p1 -u Administrator:password -H 'content-type: application/json' -d 'function ptc1(a, b) {
var qi0 = START TRANSACTION;
var acc = []; for (const row of qi0) { acc.push(row); }
var qi1 = INSERT INTO b VALUES(UUID(), {"radius": $a, "area": $b}) RETURNING meta().id,* ;
for (const row of qi1) { acc.push(row); }
var a2 = a * a; var b2 = b * b;
var qi2 = INSERT INTO b VALUES(UUID(), {"radius": $a2, "area": $b2}) RETURNING meta().id,* ;
for (const row of qi2) { acc.push(row); }
var qi9 = COMMIT ;
for (const row of qi9) { acc.push(row); }
return acc;
}'
JavaScript UDF与CURL()函数的区别
Couchbase N1QL中的JavaScript UDF与CURL()函数不同在于:
CURL()函数用于向外部服务发出HTTP请求并从web检索数据,而JavaScript UDF允许用JavaScript编写自定义逻辑并将其作为N1QL查询的一部分执行。
JavaScript本身可以调用CURL()函数。
此外,CURL()函数从外部服务返回数据,而JavaScript UDF可以返回任何可以用JavaScript表示的值,例如数字、字符串、对象、数组等。
总之,Couchbase N1QL中的JavaScript UDF非常强大,通过JavaScript编写自定义逻辑扩展了N1QL的功能,具有很大的灵活性,可用于执行内置N1QL函数无法实现的复杂操作。
JavaScript UDF与CURL()函数的用法
从Couchbase N1QL调用OpenAI的ChatGPT,可以使用CURL()函数或JavaScript UDF来实现。两种方法语法不同,下面简单说明一下。
使用CURL()函数从Couchbase N1QL调用ChatGPT的语法如下:
SELECT CURL("https://api.openai.com/v1/engines/davinci/completions", {
"request": "POST",
"headers": [
"Authorization: Bearer ChangeThisToYourKey",
"Content-Type: application/json"
],
"data": '{
"prompt": "Hello. How are you doing today?",
"temperature": 0.9,
"max_tokens": 200
}'
}) AS result ;
在此示例中,CURL()函数向ChatGPT的OpenAI API端点发出POST请求。
headers选项将Content类型设置为application/json,data选项指定请求负载,包括ChatGPT、温度和要生成的最大令牌数这些提示。
API的响应存储在响应变量中。
从Couchbase N1QL调用ChatGPT的另一种方法是使用JavaScript UDF。使用JavaScript UDF从Couchbase N1QL调用ChatGPT的语法如下:
curl -v -X POST http://localhost:8093/evaluator/v1/libraries/cglib -u Administrator:password -d 'function chatGPT3(prompt) {
var chaturl = "https://api.openai.com/v1/engines/davinci/completions"
dstr = "{\"prompt\": \"" + prompt + "\",\"temperature\": 0.5 , \"max_tokens\": 200}"
var options = {
"request" : "POST",
headers: [
"Authorization: Bearer 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",
"Content-Type: application/json"
],
"data": dstr
};
var query = SELECT CURL($chaturl, $options);
var acc = [];
for (const row of query)
acc.push(row);
return acc;
}'
在 cbq shell 中,执行以下操作:
DROP FUNCTION chatGPT3;
CREATE FUNCTION chatGPT3(prompt) language javascript as "chatGPT3" at "cglib";
select chatGPT3("Write an essay on Lincoln.")
查询结果如下:
cbq> select chatGPT3("Write an essay on Lincoln.");
{
"requestID": "6acb9a20-93f0-41c2-bdc4-fe28107d85a9",
"signature": {
"$1": "json"
},
"results": [
{
"$1": [
{
"$1": {
"choices": [
{
"finish_reason": "length",
"index": 0,
"logprobs": null,
"text": "\n\n“Lincoln,” wrote Carl Sandburg, “was a self-made man in the best sense of the word.”\n\nWhat does Sandburg mean by this statement?\n\nWhat advantages did Lincoln have in his youth?\n\nWhat disadvantages did he have?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\nWhat events from his youth helped to make him a self-made man?\n\n"
}
],
"created": 1675310533,
"id": "cmpl-6fL5Zu1MGjNorxBOwyg3H1ODzzjSD",
"model": "davinci",
"object": "text_completion",
"usage": {
"completion_tokens": 200,
"prompt_tokens": 6,
"total_tokens": 206
}
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "7.179456323s",
"executionTime": "7.179347355s",
"resultCount": 1,
"resultSize": 1537,
"serviceLoad": 1
}
}
cbq>
chatGPT实际写的内容是
"text": "\n\n“Lincoln,” wrote Carl Sandburg, “was a self-made man in the best sense of the word.”
What does Sandburg mean by this statement?
What advantages did Lincoln have in his youth?
What disadvantages did he have?
What events from his youth helped to make him a self-made man?
What events from his youth helped to make him a self-made man?
What events from his youth helped to make him a self-made man?
What events from his youth helped to make him a self-made man?
此示例创建了一个名为chatGPT的JavaScript UDF,接受prompt参数。其JavaScript代码使用请求库向ChatGPT的OpenAI API端点发出POST请求。请求的选项包括URL、method、头和请求有效负载。
进行API调用后,来自API的响应将存储在响应变量中。然后,UDF返回响应,我们可以通过调用chatGPT函数并传递必要的参数在N1QL查询中使用该响应。
JavaScript UDF与CURL()函数的限制
虽然,使用CURL()函数以及通过JavaScript UDF从Couchbase N1QL调用ChatGPT可以为应用程序提供很多好处,但是,使用这些方法时要记住一些重要事项:
CURL()函数的限制:
N1QL中的CURL()函数仅限于发出GET和POST请求。如果需要发出其他类型的请求,例如PUT或DELETE,则需要使用JavaScript UDF。
CURL()函数是同步的,在收到响应之前将阻止N1QL查询。这可能会影响性能,尤其是在响应较大或较慢的情况下。
数据大小的限制:
CURL()函数和JavaScript UDF对可以传递或接收的数据量都有限制。因此可能需要将数据分块,如果数据非常大,则需要发出多个请求。
ChatGPT的响应也可能很大,因此在规划集成时要考虑到这一点。可能需要分块处理响应并将结果存储在Couchbase,而不是直接将整个响应加载到内存中。
安全注意事项:
使用CURL()函数或JavaScript UDF向ChatGPT发出请求时,网络将传递敏感信息,例如API密钥和其他凭据。考虑信息的安全性,并在必要时采取措施对其进行加密或保护。
ChatGPT的响应也可能包含敏感信息,因此保护存储在Couchbase中的数据并保护其免受未经授权的访问非常重要。
牢记这些限制和注意事项,可以确保我们顺利使用CURL()函数和JavaScript UDF成功集成ChatGPT到Couchbase N1QL。
结语
ChatGPT的能力令人印象深刻。虽然ChatGPT在代码生成方面仍需改进,但这是一个全新的世界。