微信扫码
与创始人交个朋友
我要投稿
这个非常实验性的扩展从 DuckDB 内部启动了一个 HTTP 客户端,用于处理 GET/POST 请求。
实验性:使用风险自负!
INSTALL http_client FROM community;
LOAD http_client;
注意:目前DuckDB 版本需要在1.1.2 才能直接下载插件,否则需要自己编译。
• http_get(url)
• http_post(url, headers, params)
WITH __input AS (
SELECT
http_get(
'https://httpbin.org/delay/0'
) AS res
),
__response AS (
SELECT
(res->>'status')::INT AS status,
(res->>'reason') AS reason,
unnest( from_json(((res->>'body')::JSON)->'headers', '{"Host": "VARCHAR"}') ) AS features
FROM
__input
)
SELECT
__response.status,
__response.reason,
__response.Host AS host
FROM
__response
;
┌────────┬─────────┬─────────────┐
│ status │ reason │ host │
│ int32 │ varchar │ varchar │
├────────┼─────────┼─────────────┤
│ 200 │ OK │ httpbin.org │
└────────┴─────────┴─────────────┘
WITH __input AS (
SELECT
http_post(
'https://httpbin.org/delay/0',
headers => MAP {
'accept': 'application/json'
},
params => MAP { }
) AS res
),
__response AS (
SELECT
(res->>'status')::INT AS status,
(res->>'reason') AS reason,
unnest( from_json(((res->>'body')::JSON)->'headers', '{"Host": "VARCHAR"}') ) AS features
FROM
__input
)
SELECT
__response.status,
__response.reason,
__response.Host AS host
FROM
__response
;
┌────────┬─────────┬─────────────┐
│ status │ reason │ host │
│ int32 │ varchar │ varchar │
├────────┼─────────┼─────────────┤
│ 200 │ OK │ httpbin.org │
└────────┴─────────┴─────────────┘
前面的一个是POST parameters, 下面的这个是POST JSON
WITH __input AS (
SELECT
http_post(
'https://earth-search.aws.element84.com/v0/search',
headers => MAP {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip',
'Accept': 'application/geo+json'
},
params => {
'collections': ['sentinel-s2-l2a-cogs'],
'ids': ['S2A_56LPN_20210930_0_L2A'],
'datetime': '2021-09-30/2021-09-30',
'limit': 10
}
) AS res
),
__response AS (
SELECT
unnest( from_json(((res->>'body')::JSON)->'features', '["json"]') ) AS features
FROM
__input
)
SELECT
features->>'id' AS id
FROM
__response
;
http_post 部分等价于CURL.的
curl -X POST https://earth-search.aws.element84.com/v0/search \
-H "Content-Type: application/json" \
-H "Accept-Encoding: gzip" \
-H "Accept: application/geo+json" \
-d '{
"collections": ["sentinel-s2-l2a-cogs"],
"ids": ["S2A_56LPN_20210930_0_L2A"],
"datetime": "2021-09-30/2021-09-30",
"limit": 10
}
'
这是 @ahuarte47 提供的原始示例,启发了这个社区扩展。
INSTALL json;
INSTALL httpfs;
INSTALL spatial;
LOAD json;
LOAD httpfs;
LOAD spatial;
WITH __input AS (
SELECT
http_get(
'https://earth-search.aws.element84.com/v0/search'
) AS res
),
__features AS (
SELECT
unnest( from_json(((res->>'body')::JSON)->'features', '["json"]') )
AS features
FROM
__input
)
SELECT
features->>'id' AS id,
features->'properties'->>'sentinel:product_id' AS product_id,
concat(
'T',
features->'properties'->>'sentinel:utm_zone',
features->'properties'->>'sentinel:latitude_band',
features->'properties'->>'sentinel:grid_square'
) AS grid_id,
ST_GeomFromGeoJSON(features->'geometry') AS geom
FROM
__features
;
┌──────────────────────┬──────────────────────┬─────────┬──────────────────────────────────────────────────────────────────────────────────┐
│ id │ product_id │ grid_id │ geom │
│ varchar │ varchar │ varchar │ geometry │
├──────────────────────┼──────────────────────┼─────────┼──────────────────────────────────────────────────────────────────────────────────┤
│ S2B_55GDP_20241003… │ S2B_MSIL2A_2024100… │ T55GDP │ POLYGON ((146.7963024570636 -42.53859799130381, 145.7818492341335 -42.53284395… │
│ S2B_55HEC_20241003… │ S2B_MSIL2A_2024100… │ T55HEC │ POLYGON ((146.9997932100229 -34.429312828654396, 146.9997955899612 -33.4390429… │
│ S2B_55JHN_20241003… │ S2B_MSIL2A_2024100… │ T55JHN │ POLYGON ((149.9810192714723 -25.374826158099584, 149.9573295859729 -24.3845516… │
│ S2B_15MWT_20230506… │ S2B_MSIL2A_2023050… │ T15MWT │ POLYGON ((-92.01266261624052 -2.357695714729873, -92.0560908879947 -2.35076658… │
│ S2B_16PBT_20230506… │ S2B_MSIL2A_2023050… │ T16PBT │ POLYGON ((-88.74518736203468 11.690012668805194, -88.9516536515512 11.72635252… │
│ S2B_16PCT_20230506… │ S2B_MSIL2A_2023050… │ T16PCT │ POLYGON ((-87.82703591176752 11.483638069337541, -88.8349824533826 11.70734355… │
│ S2B_15PZP_20230506… │ S2B_MSIL2A_2023050… │ T15PZP │ POLYGON ((-89.24113885498912 11.784951995968179, -89.38831685490888 11.8080246… │
│ S2B_16PET_20230506… │ S2B_MSIL2A_2023050… │ T16PET │ POLYGON ((-87.00017408768262 11.277451946475995, -87.00017438483464 11.7600349… │
│ S2B_16PBU_20230506… │ S2B_MSIL2A_2023050… │ T16PBU │ POLYGON ((-88.74518962519173 11.690373971442378, -89.62017907866615 11.8466519… │
│ S2B_16PDU_20230506… │ S2B_MSIL2A_2023050… │ T16PDU │ POLYGON ((-87.91783982214183 11.670141095427311, -87.92096676562824 12.5828090… │
└──────────────────────┴──────────────────────┴─────────┴──────────────────────────────────────────────────────────────────────────────────┘
CREATE OR REPLACE MACRO prompt(q) AS (
WITH __input AS (
SELECT
http_post(
'https://api.siliconflow.cn/v1/chat/completions',
headers => MAP {
'Authorization': 'Bearer sk-xxxiobybdngkkkrodyphzwrvroozhgtzdsfkcpumrrtrxxxx',
'Content-Type': 'application/json; charset=UTF-8'
},
params =>{
'model': 'Qwen/Qwen2.5-7B-Instruct',
'messages': [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': q}
]
}
) AS res
)
select json_extract(((table __input)->>'body')::JSON ,'$.choices[0].message.content') as chat_response
);
select prompt('"介绍下 DuckDB"') as answer
DuckDB 是一个高性能的嵌入式 SQL 数据库,它可以在多个编程语言中轻松使用,支持各种数据源,并且可以进行内存计算和外部存储计算。...
WITH question as (SELECT *
FROM (VALUES ('美国在首府?不超过10个字?', 1), ('大熊猫是哪个国家的国宝?不超过10个字', 2)) question(q, id))
SELECT id,q, prompt(q) as a FROM question;
q | id | a |
0 | 美国在首府?不超过10个字 | 华盛顿特区是美国首府。 |
1 | 大熊猫是哪个国家的国宝?不超过10个字 | 大熊猫是中国的国宝。 |
CREATE OR REPLACE MACRO embedding(q) AS (
WITH __input AS (
SELECT
http_post(
'https://api.siliconflow.cn/v1/embeddings',
headers => MAP {
'Authorization': 'Bearer sk-xxxiobybdngkkkrodyphzwrvroozhgtzdsfkcpumrrtrxxxx',
'Content-Type': 'application/json; charset=UTF-8'
},
params =>{
'model': 'BAAI/bge-large-zh-v1.5',
'input': q,
'encoding_format':'float'
}
) AS res
)
-- SELECT (table __input)
SELECT json_extract(((table __input)->>'body')::JSON ,'$.data[0].embedding') as chat_response
);
select embedding('"介绍下 DuckDB"') as embedding
[0.0024559293,0.0032171067,-0.032328855,0.0028547805,0.0151242,0.007012767,-0.03020165,...]
http_get 缺少headers和parameters 的支持 比如我想获得所有的模型
curl https://api.siliconflow.cn/v1/models \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxiobybdngkkkrodyphzwrvroozhgtzdsfkcpumrrtrxxxx"
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-11-14
Alibaba开源UReader:通用免OCR文档理解
2024-11-13
这家小公司的700 亿参数模型竟然比Open o1还强?
2024-11-13
通义牛逼!!比肩Chatgpt-4o 还自带 Artifact
2024-11-13
Ollama 更新!本地跑 LLama3.2,轻量级+视觉能力,能媲美GPT-4o?
2024-11-13
开源版SearchGPT来了,两张3090就可复现,超越Perplexity付费版
2024-11-12
字节跳动AI社区新动作:炉米Lumi,一站式模型共创平台揭秘
2024-11-12
我们是这样做大模型应用的(文末有惊喜)
2024-11-12
Qwen新作,代码界的王!
2024-05-06
2024-08-13
2024-07-25
2024-06-12
2024-06-16
2024-07-11
2024-07-20
2024-06-15
2024-07-25
2024-07-25
2024-11-13
2024-11-13
2024-10-07
2024-09-22
2024-09-20
2024-09-14
2024-09-14
2024-09-12