simonw / datasette-openai

SQL functions for calling OpenAI APIs
https://datasette.io/plugins/datasette-openai
Apache License 2.0
21 stars 3 forks source link

Initial prototype #1

Closed simonw closed 1 year ago

simonw commented 1 year ago

The initial goal of this plugin is to help me build a prototype of semantic search using embeddings - to work against a table created using this tool:

So two SQL functions: openai_embedding(text, api_key) and openai_embedding_similarity(a, b).

simonw commented 1 year ago

This worked:

with query as (
  select
    openai_embedding(:query, :token) as q
),
top_n as (
  select
    id,
    openai_embedding_similarity(query.q, embedding) as score
  from
    blog.embeddings
    ,query
  order by
    score desc
  limit
    10
)
select
  blog_entry.id,
  blog_entry.title,
  top_n.score
from
  simonwillisonblog.blog_entry
  join top_n on blog_entry.id = top_n.id
order by
  score desc

With Datasette run using:

datasette ../openai-to-sqlite/blog.db simonwillisonblog.db \
  --crossdb -p 8003 --setting sql_time_limit_ms 10000