gabykim commited on
Commit
1760e74
Β·
1 Parent(s): e1438e8

remove code path prefix if configured

Browse files
src/know_lang_bot/chat_bot/chat_interface.py CHANGED
@@ -1,5 +1,6 @@
1
  from dataclasses import dataclass
2
  import gradio as gr
 
3
  from know_lang_bot.configs.config import AppConfig
4
  from know_lang_bot.utils.fancy_log import FancyLogger
5
  from know_lang_bot.utils.rate_limiter import RateLimiter
@@ -19,32 +20,12 @@ class CodeContext:
19
  start_line: int
20
  end_line: int
21
 
22
- def to_title(self) -> str:
23
  """Format code context as a title string"""
24
- title = f"πŸ“„ {self.file_path} (lines {self.start_line}-{self.end_line})"
 
25
  return title
26
 
27
- @classmethod
28
- def from_title(cls, title: str) -> "CodeContext":
29
- """Parse code context from a title string"""
30
- try:
31
- # Split on first emoji and space
32
- parts = title.split("πŸ“„ ")[1].split(" ")
33
- file_path = parts[0]
34
- # Extract line numbers from parentheses
35
- line_nums = parts[1].strip("()").split(" ")[1].split("-")
36
- start_line = int(line_nums[0])
37
- end_line = int(line_nums[1])
38
-
39
- return cls(
40
- file_path=file_path,
41
- start_line=start_line,
42
- end_line=end_line,
43
- )
44
- except Exception as e:
45
- LOG.error(f"Error parsing code context from title: {e}")
46
- return None
47
-
48
  @classmethod
49
  def from_metadata(cls, metadata: Dict) -> "CodeContext":
50
  """Create code context from metadata dictionary"""
@@ -94,7 +75,7 @@ class CodeQAChatInterface:
94
  if not code:
95
  return None
96
 
97
- return f"<details><summary>{context.to_title()}</summary>\n\n```python\n{code}\n```\n\n</details>"
98
 
99
  def _handle_feedback(self, like_data: gr.LikeData, history: List[ChatMessage], request: gr.Request):
100
  # Get the query and response pair
 
1
  from dataclasses import dataclass
2
  import gradio as gr
3
+ from know_lang_bot.configs.chat_config import ChatConfig
4
  from know_lang_bot.configs.config import AppConfig
5
  from know_lang_bot.utils.fancy_log import FancyLogger
6
  from know_lang_bot.utils.rate_limiter import RateLimiter
 
20
  start_line: int
21
  end_line: int
22
 
23
+ def to_title(self, config: ChatConfig) -> str:
24
  """Format code context as a title string"""
25
+ truncated_file_path = self.file_path[len(config.code_path_prefix):]
26
+ title = f"πŸ“„ {truncated_file_path} (lines {self.start_line}-{self.end_line})"
27
  return title
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  @classmethod
30
  def from_metadata(cls, metadata: Dict) -> "CodeContext":
31
  """Create code context from metadata dictionary"""
 
75
  if not code:
76
  return None
77
 
78
+ return f"<details><summary>{context.to_title(self.config.chat)}</summary>\n\n```python\n{code}\n```\n\n</details>"
79
 
80
  def _handle_feedback(self, like_data: gr.LikeData, history: List[ChatMessage], request: gr.Request):
81
  # Get the query and response pair
src/know_lang_bot/configs/chat_config.py CHANGED
@@ -3,10 +3,35 @@ from pydantic_settings import BaseSettings
3
  from enum import Enum
4
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  class AnalyticsProvider(str, Enum):
7
  MIXPANEL = "mixpanel"
8
 
9
-
10
  class ChatbotAnalyticsConfig(BaseSettings):
11
  enabled: bool = Field(
12
  default=False,
 
3
  from enum import Enum
4
 
5
 
6
+ class ChatConfig(BaseSettings):
7
+ max_context_chunks: int = Field(
8
+ default=5,
9
+ description="Maximum number of similar chunks to include in context"
10
+ )
11
+ similarity_threshold: float = Field(
12
+ default=0.7,
13
+ description="Minimum similarity score to include a chunk"
14
+ )
15
+ interface_title: str = Field(
16
+ default="Code Repository Q&A Assistant",
17
+ description="Title shown in the chat interface"
18
+ )
19
+ interface_description: str = Field(
20
+ default="Ask questions about the codebase and I'll help you understand it!",
21
+ description="Description shown in the chat interface"
22
+ )
23
+ max_length_per_chunk: int = Field(
24
+ default=8000,
25
+ description="Maximum number of characters per chunk"
26
+ )
27
+ code_path_prefix: str = Field(
28
+ default="src/",
29
+ description="Prefix of code paths in the chat interface"
30
+ )
31
+
32
  class AnalyticsProvider(str, Enum):
33
  MIXPANEL = "mixpanel"
34
 
 
35
  class ChatbotAnalyticsConfig(BaseSettings):
36
  enabled: bool = Field(
37
  default=False,
src/know_lang_bot/configs/config.py CHANGED
@@ -4,7 +4,7 @@ from pydantic import Field, field_validator, ValidationInfo
4
  from pathlib import Path
5
  import fnmatch
6
  from know_lang_bot.core.types import ModelProvider
7
- from know_lang_bot.configs.chat_config import ChatbotAnalyticsConfig
8
  import os
9
 
10
  def _validate_api_key(v: Optional[str], info: ValidationInfo) -> Optional[str]:
@@ -175,27 +175,6 @@ class RerankerConfig(BaseSettings):
175
  def validate_api_key(cls, v: Optional[str], info: ValidationInfo) -> Optional[str]:
176
  return _validate_api_key(v, info)
177
 
178
- class ChatConfig(BaseSettings):
179
- max_context_chunks: int = Field(
180
- default=5,
181
- description="Maximum number of similar chunks to include in context"
182
- )
183
- similarity_threshold: float = Field(
184
- default=0.7,
185
- description="Minimum similarity score to include a chunk"
186
- )
187
- interface_title: str = Field(
188
- default="Code Repository Q&A Assistant",
189
- description="Title shown in the chat interface"
190
- )
191
- interface_description: str = Field(
192
- default="Ask questions about the codebase and I'll help you understand it!",
193
- description="Description shown in the chat interface"
194
- )
195
- max_length_per_chunk: int = Field(
196
- default=8000,
197
- description="Maximum number of characters per chunk"
198
- )
199
 
200
  class EvaluatorConfig(LLMConfig):
201
  evaluation_rounds: int = Field(
 
4
  from pathlib import Path
5
  import fnmatch
6
  from know_lang_bot.core.types import ModelProvider
7
+ from know_lang_bot.configs.chat_config import ChatConfig, ChatbotAnalyticsConfig
8
  import os
9
 
10
  def _validate_api_key(v: Optional[str], info: ValidationInfo) -> Optional[str]:
 
175
  def validate_api_key(cls, v: Optional[str], info: ValidationInfo) -> Optional[str]:
176
  return _validate_api_key(v, info)
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
  class EvaluatorConfig(LLMConfig):
180
  evaluation_rounds: int = Field(