ishaq101 commited on
Commit
4324a46
Β·
1 Parent(s): d911d2b

[NOTICKET] Fix: model cost display in langfuse

Browse files
externals/storages/azure_blob.py CHANGED
@@ -10,28 +10,6 @@ from utils.logger import get_logger
10
  logger = get_logger("azure-blob")
11
 
12
 
13
- # async def get_blob_service_client() -> BlobServiceClient:
14
- # try:
15
- # # credential = AzureSasCredential(AzureBlobConstants.SAS_KEY)
16
- # credential = AzureBlobConstants.SAS_KEY
17
- # logger.info(f"βœ… Initialized Azure AI Cred: Using **Azure SAS Key Credential**")
18
-
19
- # _blob_service_client = BlobServiceClient(
20
- # account_url=AzureBlobConstants.ENDPOINT,
21
- # credential=credential,
22
- # )
23
- # return _blob_service_client
24
- # except Exception as E:
25
- # logger.error(f'❌ Getting blob service client error, {E}')
26
- # logger.error(f'⚠️ using DefaultAzureCredential')
27
- # credential=DefaultAzureCredential()
28
- # _blob_service_client = BlobServiceClient(
29
- # account_url=AzureBlobConstants.ENDPOINT,
30
- # credential=credential,
31
- # )
32
- # return _blob_service_client
33
-
34
-
35
  async def get_container_client() -> ContainerClient:
36
  try:
37
  container_client = ContainerClient(
@@ -39,6 +17,7 @@ async def get_container_client() -> ContainerClient:
39
  container_name=AzureBlobConstants.CONTAINER_NAME,
40
  credential=AzureSasCredential(AzureBlobConstants.SAS_KEY)
41
  )
 
42
  return container_client
43
  except Exception as E:
44
  logger.error(f'❌ Getting container client error, {E}')
@@ -50,19 +29,6 @@ async def get_container_client() -> ContainerClient:
50
  )
51
  return container_client
52
 
53
- # async def get_container_client() -> ContainerClient:
54
- # service = await get_blob_service_client()
55
- # container = service.get_container_client(
56
- # AzureBlobConstants.CONTAINER_NAME
57
- # )
58
-
59
- # try:
60
- # await container.create_container()
61
- # logger.info("βœ… Blob container created")
62
- # except Exception:
63
- # pass # already exists
64
-
65
- # return container
66
 
67
 
68
  # ---------- HELPERS ----------
@@ -71,49 +37,7 @@ def build_blob_path(filename: str) -> str:
71
 
72
 
73
  # ---------- UPLOAD ----------
74
- # async def upload_pdf(
75
- # file: UploadFile,
76
- # ) -> Dict[str, Any]:
77
-
78
- # if file.content_type != "application/pdf":
79
- # raise HTTPException(
80
- # status_code=status.HTTP_400_BAD_REQUEST,
81
- # detail="Only PDF files are allowed",
82
- # )
83
-
84
- # container = await get_container_client()
85
-
86
- # blob_name = f"{uuid.uuid4()}.pdf"
87
- # blob_path = build_blob_path(blob_name)
88
-
89
- # blob_client = container.get_blob_client(blob_path)
90
-
91
- # size = 0
92
- # chunks = []
93
-
94
- # async for chunk in file.stream():
95
- # size += len(chunk)
96
- # if size > AzureBlobConstants.MAX_FILE_SIZE_MB * 1024 * 1024:
97
- # raise HTTPException(
98
- # status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
99
- # detail="File too large",
100
- # )
101
- # chunks.append(chunk)
102
-
103
- # await blob_client.upload_blob(
104
- # b"".join(chunks),
105
- # overwrite=False,
106
- # content_type="application/pdf",
107
- # )
108
-
109
- # logger.info(f"βœ… Uploaded PDF: {blob_path}")
110
-
111
- # return {
112
- # "blob_name": blob_name,
113
- # "path": blob_path,
114
- # "url": blob_client.url,
115
- # "size_bytes": size,
116
- # }
117
  def helper_path_maker(
118
  tenant_id: str,
119
  user_id: str,
@@ -123,8 +47,6 @@ def helper_path_maker(
123
  return f"{in_folder}/{tenant_id}/{user_id}/{filename}"
124
 
125
 
126
-
127
-
128
  async def upload_pdf(file: UploadFile, tenant_id: str, user_id: str) -> dict:
129
  container_client = await get_container_client()
130
  blob_name = helper_path_maker(tenant_id, user_id, file.filename)
@@ -139,6 +61,7 @@ async def upload_pdf(file: UploadFile, tenant_id: str, user_id: str) -> dict:
139
  content_settings=ContentSettings(
140
  content_type=file.content_type or "application/pdf"
141
  ),
 
142
  )
143
 
144
  return {
 
10
  logger = get_logger("azure-blob")
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  async def get_container_client() -> ContainerClient:
14
  try:
15
  container_client = ContainerClient(
 
17
  container_name=AzureBlobConstants.CONTAINER_NAME,
18
  credential=AzureSasCredential(AzureBlobConstants.SAS_KEY)
19
  )
20
+ logger.info(f'βœ… Getting container client success')
21
  return container_client
22
  except Exception as E:
23
  logger.error(f'❌ Getting container client error, {E}')
 
29
  )
30
  return container_client
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
 
34
  # ---------- HELPERS ----------
 
37
 
38
 
39
  # ---------- UPLOAD ----------
40
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def helper_path_maker(
42
  tenant_id: str,
43
  user_id: str,
 
47
  return f"{in_folder}/{tenant_id}/{user_id}/{filename}"
48
 
49
 
 
 
50
  async def upload_pdf(file: UploadFile, tenant_id: str, user_id: str) -> dict:
51
  container_client = await get_container_client()
52
  blob_name = helper_path_maker(tenant_id, user_id, file.filename)
 
61
  content_settings=ContentSettings(
62
  content_type=file.content_type or "application/pdf"
63
  ),
64
+ max_concurrency=4,
65
  )
66
 
67
  return {
services/agentic/profile_scoring.py CHANGED
@@ -408,8 +408,8 @@ class AgenticScoringService:
408
  llm=llm,
409
  input_llm=input_llm,
410
  metadata_observability=MetadataObservability(
411
- fullname="system",
412
- task_id="system",
413
  agent=self._ai_matching.__name__,
414
  )
415
  )
 
408
  llm=llm,
409
  input_llm=input_llm,
410
  metadata_observability=MetadataObservability(
411
+ fullname=self.user.full_name,
412
+ task_id=self.user.user_id,
413
  agent=self._ai_matching.__name__,
414
  )
415
  )
services/base/BaseGenerator.py CHANGED
@@ -64,73 +64,23 @@ class BaseAIGenerator:
64
  """private helper for retries"""
65
  return chain.invoke(input_llm, config=config)
66
 
67
- # @trace_runtime
68
- # async def agenerate(self):
69
- # try:
70
- # chain = self.prompt | self.llm
71
- # config = {"callbacks": [langfuse_handler]}
72
-
73
- # with langfuse.start_as_current_observation(
74
- # as_type='generation',
75
- # name=self.name,
76
- # input=self.input_llm,
77
- # ) as trace:
78
- # trace.update_trace(user_id=self.metadata_observability.fullname,
79
- # session_id=self.metadata_observability.task_id,
80
- # metadata=self.metadata_observability.model_dump()
81
- # )
82
- # output = await self._asafe_invoke(chain=chain,
83
- # input_llm=self.input_llm,
84
- # config=config)
85
- # trace.update_trace(output=output)
86
- # return output
87
- # except Exception as E:
88
- # logger.error(f"❌ BaseGenerator, agenerate error, {E}")
89
- # return None
90
-
91
- # @trace_runtime
92
- # async def generate(self):
93
- # try:
94
- # chain = self.prompt | self.llm
95
- # config = {"callbacks": [langfuse_handler]}
96
-
97
- # with langfuse.start_as_current_observation(
98
- # as_type='generation',
99
- # name=self.name,
100
- # input=self.input_llm,
101
- # ) as trace:
102
- # trace.update_trace(user_id=self.metadata_observability.fullname,
103
- # session_id=self.metadata_observability.task_id,
104
- # metadata=self.metadata_observability.model_dump()
105
- # )
106
- # output = self._safe_invoke(chain=chain,
107
- # input_llm=self.input_llm,
108
- # config=config)
109
- # trace.update_trace(output=output)
110
- # return output
111
- # except Exception as E:
112
- # logger.error(f"❌ BaseGenerator, generate error, {E}")
113
- # return None
114
-
115
  @trace_runtime
116
  async def agenerate(self):
117
  trace = None
118
  try:
119
- chain = self.prompt | self.llm
120
- config = {"callbacks": [langfuse_handler]}
121
-
122
  # βœ… Create trace (no context manager, no end())
123
  trace = langfuse.trace(
124
  name=self.name,
125
  input=self.input_llm,
126
- )
127
-
128
- trace.update(
129
  user_id=self.metadata_observability.fullname,
130
  session_id=self.metadata_observability.task_id,
131
  metadata=self.metadata_observability.model_dump(),
132
  )
133
 
 
 
 
 
134
  output = await self._asafe_invoke(
135
  chain=chain,
136
  input_llm=self.input_llm,
@@ -156,20 +106,19 @@ class BaseAIGenerator:
156
  async def generate(self):
157
  trace = None
158
  try:
159
- chain = self.prompt | self.llm
160
- config = {"callbacks": [langfuse_handler]}
161
 
162
  trace = langfuse.trace(
163
  name=self.name,
164
  input=self.input_llm,
165
- )
166
-
167
- trace.update(
168
  user_id=self.metadata_observability.fullname,
169
  session_id=self.metadata_observability.task_id,
170
  metadata=self.metadata_observability.model_dump(),
171
  )
172
 
 
 
 
 
173
  output = self._safe_invoke(
174
  chain=chain,
175
  input_llm=self.input_llm,
 
64
  """private helper for retries"""
65
  return chain.invoke(input_llm, config=config)
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  @trace_runtime
68
  async def agenerate(self):
69
  trace = None
70
  try:
 
 
 
71
  # βœ… Create trace (no context manager, no end())
72
  trace = langfuse.trace(
73
  name=self.name,
74
  input=self.input_llm,
 
 
 
75
  user_id=self.metadata_observability.fullname,
76
  session_id=self.metadata_observability.task_id,
77
  metadata=self.metadata_observability.model_dump(),
78
  )
79
 
80
+ handler = trace.get_langchain_handler()
81
+ config = {"callbacks": [handler]}
82
+ chain = self.prompt | self.llm
83
+
84
  output = await self._asafe_invoke(
85
  chain=chain,
86
  input_llm=self.input_llm,
 
106
  async def generate(self):
107
  trace = None
108
  try:
 
 
109
 
110
  trace = langfuse.trace(
111
  name=self.name,
112
  input=self.input_llm,
 
 
 
113
  user_id=self.metadata_observability.fullname,
114
  session_id=self.metadata_observability.task_id,
115
  metadata=self.metadata_observability.model_dump(),
116
  )
117
 
118
+ handler = trace.get_langchain_handler()
119
+ config = {"callbacks": [handler]}
120
+ chain = self.prompt | self.llm
121
+
122
  output = self._safe_invoke(
123
  chain=chain,
124
  input_llm=self.input_llm,
services/knowledge/extract_profile.py CHANGED
@@ -149,8 +149,8 @@ class KnowledgeExtractService:
149
  input_llm=input_llm,
150
  llm=llm,
151
  metadata_observability=MetadataObservability(
152
- fullname="local test",
153
- task_id=raw_profile.get("profile_id"),
154
  agent=self.extract.__name__,
155
  )
156
  )
 
149
  input_llm=input_llm,
150
  llm=llm,
151
  metadata_observability=MetadataObservability(
152
+ fullname=self.user.full_name,
153
+ task_id=self.user.user_id,
154
  agent=self.extract.__name__,
155
  )
156
  )