taaha3244 commited on
Commit
319392e
1 Parent(s): dd9d8a6

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +37 -0
utils.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
2
+ from qdrant_client import QdrantClient
3
+
4
+
5
+ def setup_openai_embeddings(api_key):
6
+ """Set up OpenAI embeddings."""
7
+ return OpenAIEmbeddings(model='text-embedding-3-small', openai_api_key=api_key)
8
+
9
+
10
+ def setup_qdrant_client(url, api_key):
11
+ """Set up Qdrant client."""
12
+ return QdrantClient(location=url, api_key=api_key)
13
+
14
+ def format_document_metadata(docs):
15
+ """Format metadata for each document."""
16
+ formatted_docs = []
17
+ for doc in docs:
18
+ metadata_str = ', '.join(f"{key}: {value}" for key, value in doc.metadata.items())
19
+ doc_str = f"{doc.page_content}\nMetadata: {metadata_str}"
20
+ formatted_docs.append(doc_str)
21
+ return "\n\n".join(formatted_docs)
22
+
23
+ def openai_llm(api_key: str):
24
+ """Get a configured OpenAI language model."""
25
+ return ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key=api_key)
26
+
27
+ def delete_collection(collection_name, qdrant_url, qdrant_api_key):
28
+ """Delete a Qdrant collection."""
29
+ client = setup_qdrant_client(qdrant_url, qdrant_api_key)
30
+ try:
31
+ client.delete_collection(collection_name=collection_name)
32
+ except Exception as e:
33
+ print("Failed to delete collection:", e)
34
+
35
+ def is_document_embedded(filename):
36
+ """Check if a document is already embedded. Actual implementation needed."""
37
+ return False