leo-pasi commited on
Commit
cdbb4e1
·
1 Parent(s): 4fe2243

latest code from main

Browse files
configs/auto_merging.yaml CHANGED
@@ -2,7 +2,7 @@ source_doc: "Master_Thesis.pdf"
2
  rag_mode: "auto-merging retrieval"
3
  llm_openai_model: "gpt-4o-mini"
4
  embed_model: "BAAI/bge-small-en-v1.5"
5
- chunk_sizes: [2048, 512, 128]
6
- similarity_top_k: 6
7
  rerank_model: "cross-encoder/ms-marco-MiniLM-L-2-v2"
8
  rerank_top_n: 2
 
2
  rag_mode: "auto-merging retrieval"
3
  llm_openai_model: "gpt-4o-mini"
4
  embed_model: "BAAI/bge-small-en-v1.5"
5
+ chunk_sizes: [2048, 512]
6
+ similarity_top_k: 8
7
  rerank_model: "cross-encoder/ms-marco-MiniLM-L-2-v2"
8
  rerank_top_n: 2
configs/basic.yaml CHANGED
@@ -2,6 +2,6 @@ source_doc: "Master_Thesis.pdf"
2
  rag_mode: "classic retrieval"
3
  llm_openai_model: "gpt-4o-mini"
4
  embed_model: "BAAI/bge-small-en-v1.5"
5
- similarity_top_k: 6
6
  rerank_model: "cross-encoder/ms-marco-MiniLM-L-2-v2"
7
  rerank_top_n: 2
 
2
  rag_mode: "classic retrieval"
3
  llm_openai_model: "gpt-4o-mini"
4
  embed_model: "BAAI/bge-small-en-v1.5"
5
+ similarity_top_k: 10
6
  rerank_model: "cross-encoder/ms-marco-MiniLM-L-2-v2"
7
  rerank_top_n: 2
configs/sentence_window.yaml CHANGED
@@ -2,7 +2,7 @@ source_doc: "Master_Thesis.pdf"
2
  rag_mode: "sentence window retrieval"
3
  llm_openai_model: "gpt-4o-mini"
4
  embed_model: "BAAI/bge-small-en-v1.5"
5
- sentence_window_size: 3
6
  similarity_top_k: 6
7
  rerank_model: "cross-encoder/ms-marco-MiniLM-L-2-v2"
8
  rerank_top_n: 2
 
2
  rag_mode: "sentence window retrieval"
3
  llm_openai_model: "gpt-4o-mini"
4
  embed_model: "BAAI/bge-small-en-v1.5"
5
+ sentence_window_size: 4
6
  similarity_top_k: 6
7
  rerank_model: "cross-encoder/ms-marco-MiniLM-L-2-v2"
8
  rerank_top_n: 2
scripts/app.py CHANGED
@@ -112,7 +112,7 @@ with open(welcome_message_path, encoding="utf-8") as f:
112
  gradio_app = gr.Interface(
113
  fn=chat_bot,
114
  inputs=[
115
- gr.Textbox(placeholder=default_message, label="Query"),
116
  gr.Dropdown(
117
  choices=SupportedRags.__args__,
118
  label="RAG mode",
 
112
  gradio_app = gr.Interface(
113
  fn=chat_bot,
114
  inputs=[
115
+ gr.Textbox(placeholder=default_message, label="Query", lines=2),
116
  gr.Dropdown(
117
  choices=SupportedRags.__args__,
118
  label="RAG mode",
spaces/welcome_message.md CHANGED
@@ -11,7 +11,7 @@ Here you get to choose between three RAG techniques:
11
  - **auto-merging retrieval**
12
 
13
  Feel free to experiment with different modes! Note that a little extra delay is to be expected when switching to another mode.
14
- Also, note that all your queries (as well as system responses) are automatically logged on a remote PostgreSQL database for continuous monitoring of the deployed systems.
15
 
16
  Each of these systems has been optimized for performance by doing a grid search on the
17
  relevant parameters. Performance is quantified with five metrics:
 
11
  - **auto-merging retrieval**
12
 
13
  Feel free to experiment with different modes! Note that a little extra delay is to be expected when switching to another mode.
14
+ Also, note that all your queries (as well as system responses, and evaluation of these responses) are automatically logged on a remote PostgreSQL database for continuous monitoring of the deployed systems.
15
 
16
  Each of these systems has been optimized for performance by doing a grid search on the
17
  relevant parameters. Performance is quantified with five metrics:
src/mythesis_chatbot/evaluation.py CHANGED
@@ -1,9 +1,11 @@
 
1
  from pathlib import Path
 
2
 
3
  import numpy as np
4
  from tqdm import tqdm
5
  from trulens.apps.llamaindex import TruLlama
6
- from trulens.core import Feedback
7
  from trulens.providers.openai import OpenAI
8
 
9
  from src.mythesis_chatbot.utils import get_config_hash
@@ -23,7 +25,7 @@ def run_evals(eval_questions_path: Path, tru_recorder, query_engine):
23
 
24
 
25
  # Feedback function
26
- def f_answer_relevance(provider=OpenAI(), name="Answer Relevance"):
27
  return Feedback(provider.relevance_with_cot_reasons, name=name).on_input_output()
28
 
29
 
@@ -32,7 +34,7 @@ def f_context_relevance(
32
  provider=OpenAI(),
33
  context=TruLlama.select_source_nodes().node.text,
34
  name="Context Relevance",
35
- ):
36
  return (
37
  Feedback(provider.relevance, name=name)
38
  .on_input()
@@ -46,7 +48,7 @@ def f_groundedness(
46
  provider=OpenAI(),
47
  context=TruLlama.select_source_nodes().node.text,
48
  name="Groundedness",
49
- ):
50
  return (
51
  Feedback(
52
  provider.groundedness_measure_with_cot_reasons,
@@ -59,7 +61,7 @@ def f_groundedness(
59
 
60
  def get_prebuilt_trulens_recorder(
61
  query_engine, query_engine_config: dict[str, str | int]
62
- ):
63
  app_name = query_engine_config["rag_mode"]
64
  app_version = get_config_hash(query_engine_config)
65
 
@@ -71,3 +73,54 @@ def get_prebuilt_trulens_recorder(
71
  feedbacks=[f_answer_relevance(), f_context_relevance(), f_groundedness()],
72
  )
73
  return tru_recorder
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  from pathlib import Path
3
+ from typing import Literal
4
 
5
  import numpy as np
6
  from tqdm import tqdm
7
  from trulens.apps.llamaindex import TruLlama
8
+ from trulens.core import Feedback, TruSession
9
  from trulens.providers.openai import OpenAI
10
 
11
  from src.mythesis_chatbot.utils import get_config_hash
 
25
 
26
 
27
  # Feedback function
28
+ def f_answer_relevance(provider=OpenAI(), name="Answer Relevance") -> Feedback:
29
  return Feedback(provider.relevance_with_cot_reasons, name=name).on_input_output()
30
 
31
 
 
34
  provider=OpenAI(),
35
  context=TruLlama.select_source_nodes().node.text,
36
  name="Context Relevance",
37
+ ) -> Feedback:
38
  return (
39
  Feedback(provider.relevance, name=name)
40
  .on_input()
 
48
  provider=OpenAI(),
49
  context=TruLlama.select_source_nodes().node.text,
50
  name="Groundedness",
51
+ ) -> Feedback:
52
  return (
53
  Feedback(
54
  provider.groundedness_measure_with_cot_reasons,
 
61
 
62
  def get_prebuilt_trulens_recorder(
63
  query_engine, query_engine_config: dict[str, str | int]
64
+ ) -> TruLlama:
65
  app_name = query_engine_config["rag_mode"]
66
  app_version = get_config_hash(query_engine_config)
67
 
 
73
  feedbacks=[f_answer_relevance(), f_context_relevance(), f_groundedness()],
74
  )
75
  return tru_recorder
76
+
77
+
78
+ def get_tru_session(database: Literal["prod", "dev"]) -> TruSession:
79
+
80
+ print(f"Connecting to {database.lower()} database...")
81
+
82
+ match database.lower():
83
+ case "prod":
84
+ database_url = os.getenv("SUPABASE_PROD_CONNECTION_STRING_IPV4")
85
+ if database_url is None:
86
+ raise RuntimeError(
87
+ "IPv4 connection string to production database is not available as"
88
+ " an environment variable."
89
+ )
90
+ else:
91
+ print("Using IPv4 connection string...")
92
+ tru = TruSession(database_url=database_url)
93
+ return tru
94
+
95
+ case "dev":
96
+ database_url = os.getenv("SUPABASE_DEV_CONNECTION_STRING_IPV6")
97
+ if database_url:
98
+ try:
99
+ print("Using IPv6 connection string...")
100
+ tru = TruSession(database_url=database_url)
101
+ return tru
102
+ except Exception as e:
103
+ print(
104
+ "An error occurred while connecting to remote dev database with"
105
+ f" IPv6 connection string: {e}"
106
+ )
107
+ print("Reverting to IPv4")
108
+ else:
109
+ print(
110
+ "IPv6 connection string to dev database is not available as an"
111
+ " environment variable. Reverting to IPv4."
112
+ )
113
+
114
+ database_url = os.getenv("SUPABASE_DEV_CONNECTION_STRING_IPV4")
115
+ if database_url is None:
116
+ raise RuntimeError(
117
+ "IPv4 connection string to dev database is not available"
118
+ " as an environment variable."
119
+ )
120
+ else:
121
+ tru = TruSession(database_url=database_url)
122
+ return tru
123
+ case _:
124
+ raise ValueError(
125
+ f"Invalid database: {database}. Choose betwen 'prod' and 'dev'"
126
+ )