File size: 930 Bytes
16601c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import streamlit as st
from langchain_community.vectorstores import FAISS
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings

# Example queries remain the same
examples = [
    {
        "input": "Retrieve the user  who have placed the highest total value of orders.",
        "query": "SELECT u.username, SUM(p.price) AS total_order_value FROM users u JOIN orders o ON u.user_id = o.user_id JOIN products p ON o.product_id = p.product_id GROUP BY u.username ORDER BY total_order_value DESC LIMIT 10;"
    },
    # ... (rest of the examples)
]

@st.cache_resource
def get_example_selector(api_key):
    embeddings = OpenAIEmbeddings(api_key=api_key)
    example_selector = SemanticSimilarityExampleSelector.from_examples(
        examples,
        embeddings,
        FAISS,
        k=2,
        input_variables=["input"],
    )
    return example_selector