Gurjot Singh commited on
Commit
a5e91aa
·
1 Parent(s): ae4419c

Add example usage for separate keyword extraction of user's query

Browse files
examples/query_keyword_separation_example.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import asyncio
3
+ from lightrag import LightRAG, QueryParam
4
+ from lightrag.utils import EmbeddingFunc
5
+ import numpy as np
6
+ from dotenv import load_dotenv
7
+ import logging
8
+ from openai import AzureOpenAI
9
+
10
+ logging.basicConfig(level=logging.INFO)
11
+
12
+ load_dotenv()
13
+
14
+ AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")
15
+ AZURE_OPENAI_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT")
16
+ AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
17
+ AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
18
+
19
+ AZURE_EMBEDDING_DEPLOYMENT = os.getenv("AZURE_EMBEDDING_DEPLOYMENT")
20
+ AZURE_EMBEDDING_API_VERSION = os.getenv("AZURE_EMBEDDING_API_VERSION")
21
+
22
+ WORKING_DIR = "./dickens"
23
+
24
+ if os.path.exists(WORKING_DIR):
25
+ import shutil
26
+
27
+ shutil.rmtree(WORKING_DIR)
28
+
29
+ os.mkdir(WORKING_DIR)
30
+
31
+
32
+ async def llm_model_func(
33
+ prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
34
+ ) -> str:
35
+ client = AzureOpenAI(
36
+ api_key=AZURE_OPENAI_API_KEY,
37
+ api_version=AZURE_OPENAI_API_VERSION,
38
+ azure_endpoint=AZURE_OPENAI_ENDPOINT,
39
+ )
40
+
41
+ messages = []
42
+ if system_prompt:
43
+ messages.append({"role": "system", "content": system_prompt})
44
+ if history_messages:
45
+ messages.extend(history_messages)
46
+ messages.append({"role": "user", "content": prompt})
47
+
48
+ chat_completion = client.chat.completions.create(
49
+ model=AZURE_OPENAI_DEPLOYMENT, # model = "deployment_name".
50
+ messages=messages,
51
+ temperature=kwargs.get("temperature", 0),
52
+ top_p=kwargs.get("top_p", 1),
53
+ n=kwargs.get("n", 1),
54
+ )
55
+ return chat_completion.choices[0].message.content
56
+
57
+
58
+ async def embedding_func(texts: list[str]) -> np.ndarray:
59
+ client = AzureOpenAI(
60
+ api_key=AZURE_OPENAI_API_KEY,
61
+ api_version=AZURE_EMBEDDING_API_VERSION,
62
+ azure_endpoint=AZURE_OPENAI_ENDPOINT,
63
+ )
64
+ embedding = client.embeddings.create(model=AZURE_EMBEDDING_DEPLOYMENT, input=texts)
65
+
66
+ embeddings = [item.embedding for item in embedding.data]
67
+ return np.array(embeddings)
68
+
69
+
70
+ async def test_funcs():
71
+ result = await llm_model_func("How are you?")
72
+ print("Resposta do llm_model_func: ", result)
73
+
74
+ result = await embedding_func(["How are you?"])
75
+ print("Resultado do embedding_func: ", result.shape)
76
+ print("Dimensão da embedding: ", result.shape[1])
77
+
78
+
79
+ asyncio.run(test_funcs())
80
+
81
+ embedding_dimension = 3072
82
+
83
+ rag = LightRAG(
84
+ working_dir=WORKING_DIR,
85
+ llm_model_func=llm_model_func,
86
+ embedding_func=EmbeddingFunc(
87
+ embedding_dim=embedding_dimension,
88
+ max_token_size=8192,
89
+ func=embedding_func,
90
+ ),
91
+ )
92
+
93
+ book1 = open("./book_1.txt", encoding="utf-8")
94
+ book2 = open("./book_2.txt", encoding="utf-8")
95
+
96
+ rag.insert([book1.read(), book2.read()])
97
+
98
+ # Example function demonstrating the new query_with_separate_keyword_extraction usage
99
+ async def run_example():
100
+ query = "What are the top themes in this story?"
101
+ prompt = "Please simplify the response for a young audience."
102
+
103
+ # Using the new method to ensure the keyword extraction is only applied to the query
104
+ response = rag.query_with_separate_keyword_extraction(
105
+ query=query,
106
+ prompt=prompt,
107
+ param=QueryParam(mode="hybrid") # Adjust QueryParam mode as necessary
108
+ )
109
+
110
+ print("Extracted Response:", response)
111
+
112
+ # Run the example asynchronously
113
+ if __name__ == "__main__":
114
+ asyncio.run(run_example())