Skip to content

prompt and embedding function base on http_client #9

Open
@alitrack

Description

@alitrack

prompt

CREATE OR REPLACE MACRO prompt(q) AS ( 
WITH __input AS (
    SELECT
      http_post(
          
          'https://api.openai.com/v1/chat/completions',
          headers => MAP {
            'Authorization': 'Bearer sk-your_openai_key',
            '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('"introduce DuckDB"') as answer

embedding

CREATE OR REPLACE MACRO embedding(q) AS ( 
WITH  __input AS (
    SELECT
      http_post(
          'https://api.openai.com/v1/embeddings',
          headers => MAP {
            'Authorization': 'Bearer sk_your_openai_key',
            '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

for Chinese details, please visit
https://mp.weixin.qq.com/s?__biz=MzU1NTg2ODQ5Nw==&mid=2247490026&idx=1&sn=6c619eb49cf4e770fc84e60b37666d25&chksm=fbcc91d4ccbb18c2a3e4e1945caff20bdae2a2e3da16a28afcb72165228c2354aa4a6b6b6c3d&token=499112804&lang=zh_CN#rd

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions