Add this file to your AI assistant's system prompt or context to help it avoid common AI writing patterns. Source: tropes.fyi by ossama.is
This file contains hidden or 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
| def semantic_cached_prompt(prompt): | |
| """Retrieve a response from ScyllaDB or ask OpenAI if it's a new prompt. | |
| Args: | |
| prompt (str): The user prompt. | |
| Returns: | |
| str: The response to the prompt. | |
| """ | |
| embedding = create_embedding(prompt) | |
| cached_response = search_cache(embedding, threshold=0.80) | |
| if cached_response: |
This file contains hidden or 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
| def calc_cosine_similarity(self, vec1, vec2): | |
| """Calculate cosine similarity between two vectors.""" | |
| v1, v2 = np.array(vec1), np.array(vec2) | |
| return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) | |
| def search_cache(self, embedding, threshold=0.80): | |
| """ | |
| Returns the most similar response if it is above the threshold. Otherwise returns `None`. | |
| """ | |
| k = 1 |
This file contains hidden or 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
| # pip install sentence-transformers | |
| from sentence_transformers import SentenceTransformer | |
| embedding_model = SentenceTransformer('all-MiniLM-L6-v2') | |
| embedding = embedding_model.encode(text).tolist() |
This file contains hidden or 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
| CREATE KEYSPACE semantic_cache WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; | |
| CREATE TABLE semantic_cache.prompts ( | |
| prompt_id uuid PRIMARY KEY, | |
| inserted_at timestamp, | |
| prompt_text text, | |
| prompt_embedding vector<float, 384>, | |
| llm_response text, | |
| updated_at timestamp | |
| ); |
This file contains hidden or 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
| CREATE KEYSPACE recommend WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': '3'}; | |
| CREATE TABLE recommend.movies ( | |
| id INT, | |
| release_date TIMESTAMP, | |
| title TEXT, | |
| tagline TEXT, | |
| genre TEXT, | |
| imdb_id TEXT, | |
| poster_url TEXT, |
This file contains hidden or 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
| from db.scylladb import ScyllaClient | |
| from embedding_creator import EmbeddingCreator | |
| from models import Movie | |
| class MovieRecommender: | |
| def __init__(self): | |
| self.scylla_client = ScyllaClient() | |
| self.embedding_creator = EmbeddingCreator("all-MiniLM-L6-v2") | |
This file contains hidden or 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
| from sentence_transformers import SentenceTransformer | |
| class EmbeddingCreator: | |
| def __init__(self, model_name: str = 'all-MiniLM-L6-v2'): | |
| self.embedding_model = SentenceTransformer(model_name, device='cpu') | |
| def create_embedding(self, text: str) -> list[float]: | |
| """ | |
| Get embedding for a single text input using SentenceTransformer. | |
| Returns the embedding vector. | |
| """ |
This file contains hidden or 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
| CREATE KEYSPACE ttl_example WITH replication = {'class': 'NetworkTopologyStrategy', 'replication_factor': 1}; | |
| USE ttl_example; | |
| ----------------------------------------------------------- | |
| -- BASIC USAGE | |
| ----------------------------------------------------------- | |
| -- Set deafult TTL with CREATE TABLE |
NewerOlder