Skip to content

Commit 0df91e8

Browse files
committed
fix false/404 links in reference links
1 parent f9341b1 commit 0df91e8

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

apps/meshjs-rag/app/api/v1/ask_mesh_ai.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ async def ask_mesh_ai(body: ChatCompletionRequest, credentials: HTTPAuthorizatio
5252

5353
try:
5454
question = body.messages[-1].content
55-
5655
embedded_query = await openai_service.embed_query(question)
5756
context = await get_context(embedded_query, supabase)
5857
generator = openai_service.get_answer(question=question, context=context)

apps/meshjs-rag/app/services/openai.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@
1919
Answer only with the succinct context and nothing else.
2020
"""
2121

22+
AI_PROMPT = """
23+
You are a MeshJS expert assistant. Help developers with MeshJS questions using the provided context. Use the documentation context to answer questions about MeshJS and Cardano development. Provide accurate code examples and explanations based on the context provided.
24+
25+
When answering:
26+
- Give direct, helpful answers based on the context
27+
- Include relevant code examples when available
28+
- Explain concepts clearly for developers
29+
- Include any documentation or resource links found in the context. Handle them as follows:
30+
- When you find "location: ...", include the link in your answer as a reference.
31+
- If multiple links are present, include all relevant ones.
32+
- If the context doesn't cover the question, say so clearly.
33+
- Do not invent or assume APIs, methods, or functionality not in the documentation.
34+
35+
Be concise but thorough. Focus on practical, actionable guidance for MeshJS development.
36+
"""
37+
2238
class OpenAIService:
2339
def __init__(self, openai_api_key):
2440
self.client = AsyncOpenAI(api_key=openai_api_key)
@@ -79,7 +95,7 @@ async def get_answer(self, question: str, context: str, model="gpt-4o-mini"):
7995
messages = [
8096
{
8197
"role": "system",
82-
"content": "You are a MeshJS expert assistant. Help developers with MeshJS questions using the provided context.\n\nUse the documentation context to answer questions about MeshJS and Cardano development. Provide accurate code examples and explanations based on the context provided.\n\nWhen answering:\n- Give direct, helpful answers based on the context\n- Include relevant code examples when available\n- Explain concepts clearly for developers\n- Include any links present in the context for additional resources\n- If the context doesn't cover something, say so\n- Don't make up APIs or methods not in the documentation\n\nBe concise but thorough. Focus on practical, actionable guidance for MeshJS development."
98+
"content": AI_PROMPT
8399
},
84100
{
85101
"role": "user",
@@ -99,7 +115,7 @@ async def get_mcp_answer(self, question: str, context: str, model="gpt-4o-mini")
99115
messages = [
100116
{
101117
"role": "system",
102-
"content": "You are a MeshJS expert assistant. Help developers with MeshJS questions using the provided context.\n\nUse the documentation context to answer questions about MeshJS and Cardano development. Provide accurate code examples and explanations based on the context provided.\n\nWhen answering:\n- Give direct, helpful answers based on the context\n- Include relevant code examples when available\n- Explain concepts clearly for developers\n- Include any links present in the context for additional resources\n- If the context doesn't cover something, say so\n- Don't make up APIs or methods not in the documentation\n\nBe concise but thorough. Focus on practical, actionable guidance for MeshJS development."
118+
"content": AI_PROMPT
103119
},
104120
{
105121
"role": "user",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
3+
def extract_github_links(text: str) -> List[str]:
4+
lines = text.splitlines()
5+
github_links = []
6+
7+
for line in lines:
8+
if "https://github.com/MeshJS/" in line:
9+
github_links.append(line.strip())
10+
11+
return github_links
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from typing import List
22
from supabase import AsyncClient
33

4+
from app.utils.extract_github_links import extract_github_links
45

6+
PACKAGES = ["mesh-common", "mesh-core-csl", "mesh-contract", "mesh-provider", "mesh-transaction", "mesh-wallet"]
57

68
async def get_context(embedded_query: List[float], supabase: AsyncClient) -> str:
79
response = await supabase.rpc("match_docs", {
@@ -10,9 +12,19 @@ async def get_context(embedded_query: List[float], supabase: AsyncClient) -> str
1012
"match_count": 5
1113
}).execute()
1214

13-
contextual_text = "\n\n".join([data["contextual_text"] for data in response.data]) if response.data else None
15+
final_contextual_data = ""
16+
if response.data:
17+
for data in response.data:
18+
if not str(data["filepath"]).startswith(tuple(PACKAGES)):
19+
file_location = f"location: https://meshjs.dev/{data["filepath"].replace(".mdx", "")}"
20+
else:
21+
links = "\n".join(extract_github_links(data["contextual_text"]))
22+
file_location = f"location: {links}" if links else ""
1423

15-
if contextual_text:
16-
return contextual_text
24+
contextual_data = data["contextual_text"] + "\n\n" + file_location + "\n\n" if file_location else data["contextual_text"] + "\n\n"
25+
final_contextual_data += contextual_data if contextual_data else ""
26+
27+
if final_contextual_data:
28+
return final_contextual_data
1729
else:
18-
return None
30+
return "No relevant context found."

0 commit comments

Comments
 (0)