-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
203 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
To run the PGSync demo: | ||
|
||
- make sure you have bootstrapped your project and populated or Elasticsearch/Opensearch | ||
- `cd pgsync` | ||
- `source source <path/to/virtualenv>/bin/activate` | ||
- `source .pythonpath` | ||
- `./bin/es_mapping -c /path/to/your/schema.json` | ||
- `cd demo` | ||
- `pip install -r requirements.txt` | ||
- `./runserver.sh` | ||
|
||
Then open your browser and go to `http://127.0.0.1:5000/` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from functools import lru_cache | ||
|
||
from plugins.openai_plugin import OpenAIPlugin | ||
|
||
|
||
@lru_cache | ||
def get_embedding(text: str) -> list: | ||
plugin: OpenAIPlugin = OpenAIPlugin() | ||
return plugin.get_embedding(text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,18 +9,20 @@ | |
font-family: helvetica, sans-serif; | ||
line-height: 1.4; | ||
} | ||
|
||
.card { | ||
background: #fff; | ||
border-radius: 2px; | ||
display: inline-block; | ||
height: 300px; | ||
margin: 1rem; | ||
position: relative; | ||
width: 300px; | ||
background: #fff; | ||
border-radius: 2px; | ||
display: inline-block; | ||
height: 300px; | ||
margin: 1rem; | ||
position: relative; | ||
width: 300px; | ||
} | ||
|
||
h2 { | ||
color: white; | ||
text-align:center; | ||
text-align: center; | ||
display: block; | ||
font-size: 1.5em; | ||
margin-top: 1.83em; | ||
|
@@ -30,30 +32,39 @@ | |
font-weight: bold; | ||
} | ||
</style> | ||
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons"> | ||
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/bootstrap-material-design.min.css" integrity="sha384-wXznGJNEXNG1NFsbm0ugrLFMQPWswR3lds2VeinahP8N0zJw9VWSopbjv2x7WCvX" crossorigin="anonymous"> | ||
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons"> | ||
<link rel="stylesheet" | ||
href="https://unpkg.com/[email protected]/dist/css/bootstrap-material-design.min.css" | ||
integrity="sha384-wXznGJNEXNG1NFsbm0ugrLFMQPWswR3lds2VeinahP8N0zJw9VWSopbjv2x7WCvX" crossorigin="anonymous"> | ||
</head> | ||
|
||
<body> | ||
<h1>PGSync typeahead demo</h1> | ||
|
||
<div class="bmd-form-group bmd-collapse-inline pull-xs-right" style="top: 10%; left: 10%; position: absolute;"> | ||
<button class="btn bmd-btn-icon" for="search" data-toggle="collapse" data-target="#collapse-search" aria-expanded="false" aria-controls="collapse-search"> | ||
<i class="material-icons">search</i> | ||
</button> | ||
<span id="collapse-search" class="collapse"> | ||
<input class="form-control" type="search" id="searchbox" placeholder="Enter your query..."> | ||
</span> | ||
<button class="btn bmd-btn-icon" for="search" data-toggle="collapse" data-target="#collapse-search" | ||
aria-expanded="false" aria-controls="collapse-search"> | ||
<i class="material-icons">search</i> | ||
</button> | ||
<span id="collapse-search" class="collapse"> | ||
<input class="form-control" type="search" id="searchbox" placeholder="Enter your query..."> | ||
</span> | ||
</div> | ||
|
||
<div id="results" style="top: 20%; left: 10%; position: absolute;"></div> | ||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> | ||
<script src="https://unpkg.com/[email protected]/dist/umd/popper.js" integrity="sha384-fA23ZRQ3G/J53mElWqVJEGJzU0sTs+SvzG8fXVWP+kJQ1lwFAOkcUOysnlKJC33U" crossorigin="anonymous"></script> | ||
<script src="https://unpkg.com/[email protected]/dist/js/bootstrap-material-design.js" integrity="sha384-CauSuKpEqAFajSpkdjv3z9t8E7RlpJ1UP0lKM/+NdtSarroVKu069AlsRPKkFBz9" crossorigin="anonymous"></script> | ||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" | ||
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" | ||
crossorigin="anonymous"></script> | ||
<script src="https://unpkg.com/[email protected]/dist/umd/popper.js" | ||
integrity="sha384-fA23ZRQ3G/J53mElWqVJEGJzU0sTs+SvzG8fXVWP+kJQ1lwFAOkcUOysnlKJC33U" | ||
crossorigin="anonymous"></script> | ||
<script src="https://unpkg.com/[email protected]/dist/js/bootstrap-material-design.js" | ||
integrity="sha384-CauSuKpEqAFajSpkdjv3z9t8E7RlpJ1UP0lKM/+NdtSarroVKu069AlsRPKkFBz9" | ||
crossorigin="anonymous"></script> | ||
|
||
<script> | ||
|
||
const base_url = 'http://localhost:8000/'; | ||
const base_url = 'http://localhost:5000/typeahead'; | ||
const searchbox = document.getElementById("searchbox"); | ||
|
||
let requestInFlight = null; | ||
|
@@ -71,8 +82,8 @@ <h1>PGSync typeahead demo</h1> | |
// Avoid race conditions where a slow request returns after a faster one. | ||
return; | ||
} | ||
let results = '<div class="container-fluid">' + | ||
d.map(result => ` | ||
let results = '<div class="container-fluid">' + | ||
d.map(result => ` | ||
<pre style= "color:gray; font-size: 15px"> | ||
${JSON.stringify(result, null, 4)} | ||
</pre><hr> | ||
|
@@ -83,7 +94,7 @@ <h1>PGSync typeahead demo</h1> | |
|
||
function debounce(func, wait, immediate) { | ||
let timeout; | ||
return function() { | ||
return function () { | ||
let context = this, | ||
args = arguments; | ||
let later = () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#! /bin/sh | ||
adev runserver -p 8000 server.py | ||
adev runserver -p 5000 server.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ | |
"constant_keyword", | ||
"date", | ||
"date_range", | ||
"dense_vector", | ||
"double", | ||
"double_range", | ||
"flattened", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from functools import lru_cache | ||
|
||
from openai import OpenAI | ||
|
||
from pgsync import plugin | ||
|
||
|
||
class OpenAIPlugin(plugin.Plugin): | ||
""" | ||
I am an OpenAI plugin. | ||
I generate embeddings for documents using openai's text-embedding-3-small model. | ||
`pip install openai` | ||
""" | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.client: OpenAI = OpenAI() | ||
self.model: str = "text-embedding-3-small" | ||
# vector dims must match models input dims | ||
self.vector_dims = 1536 | ||
|
||
name: str = "TextEmbedding3Small" | ||
|
||
@lru_cache | ||
def get_embedding(self, text: str) -> list: | ||
text: str = text.replace("\n", " ") | ||
return ( | ||
self.client.embeddings.create(input=[text], model=self.model) | ||
.data[0] | ||
.embedding | ||
) | ||
|
||
def transform(self, doc: dict, **kwargs) -> dict: | ||
"""Demonstrates how to generate openai embeddings and add them to the document""" | ||
fields = doc["book_title"] | ||
embedding: list = self.get_embedding(fields) | ||
if len(embedding) != self.vector_dims: | ||
raise ValueError(f"Embedding dims != {self.vector_dims}.") | ||
|
||
doc["embedding"] = embedding | ||
return doc |
Oops, something went wrong.