ay so. \

"""

# Prepare messages to pass to model# We use a delimiter to help the model understand the where the user_input

# starts and ends

messages = [

{"role": "system", "content": system_message},

{"role": "user", "content": f"{delimiter}{input}{delimiter}"},

"role": "assistant",

"content": f"Relevant ZenML documentation: \n"

+ "\n".join(doc[0] for doc in related_docs),

},

logger.debug("CONTEXT USED\n\n", messages[2]["content"], "\n\n")

return get_completion_from_messages(messages, model=model)

For the get_topn_similar_docs function, we use the embeddings generated from the documents in the index store to find the most similar documents to the query:

def get_topn_similar_docs(

query_embedding: List[float],

conn: psycopg2.extensions.connection,

n: int = 5,

include_metadata: bool = False,

only_urls: bool = False,

) -> List[Tuple]:

embedding_array = np.array(query_embedding)

register_vector(conn)

cur = conn.cursor()

if include_metadata:

cur.execute(

f"SELECT content, url FROM embeddings ORDER BY embedding <=> %s LIMIT {n}",

(embedding_array,),

elif only_urls:

cur.execute(

f"SELECT url FROM embeddings ORDER BY embedding <=> %s LIMIT {n}",

(embedding_array,),

else:

cur.execute(

f"SELECT content FROM embeddings ORDER BY embedding <=> %s LIMIT {n}",

(embedding_array,),

return cur.fetchall()

Luckily we are able to get these similar documents using a function in pgvector, a plugin package for PostgreSQL: ORDER BY embedding <=> %s orders the documents by their similarity to the query embedding. This is a very efficient way to get the most relevant documents to the query and is a great example of how we can leverage the power of the database to do the heavy lifting for us.

For the get_completion_from_messages function, we use litellm as a universal interface that allows us to use lots of different LLMs. As you can see above, the model is able to synthesize the documents it has been given and provide a response to the query.

def get_completion_from_messages(