e768098d0e
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
# Copyright (c) Microsoft Corporation.
|
|
# Licensed under the MIT License.
|
|
"""File for context getting tool."""
|
|
from typing import List
|
|
from promptflow import tool
|
|
import re
|
|
|
|
|
|
@tool
|
|
def generate_prompt_context(search_result: List[dict]) -> str:
|
|
"""Generate the context for the prompt."""
|
|
def format_doc(doc: dict):
|
|
"""Format Doc."""
|
|
return f"Content: {doc['Content']}\nSource: {doc['Source']}"
|
|
|
|
SOURCE_KEY = "source"
|
|
URL_KEY = "url"
|
|
|
|
pattern = r".+/community/"
|
|
replacement_text = "https://github.com/microsoft/promptflow/blob/main/docs/"
|
|
|
|
retrieved_docs = []
|
|
for item in search_result:
|
|
|
|
metadata = item.get("metadata", None)
|
|
content = item.get("text", "")
|
|
|
|
source = ""
|
|
if metadata is not None:
|
|
if SOURCE_KEY in metadata:
|
|
if URL_KEY in metadata[SOURCE_KEY]:
|
|
source = metadata[SOURCE_KEY][URL_KEY] or ""
|
|
|
|
source = re.sub(pattern, replacement_text, source)
|
|
|
|
retrieved_docs.append({
|
|
"Content": content,
|
|
"Source": source
|
|
})
|
|
doc_string = "\n\n".join([format_doc(doc) for doc in retrieved_docs])
|
|
return doc_string
|