Merge pull request #35 from yashshah035/openai
adding missing unicode function and openrouter configuration
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -66,7 +66,7 @@ from tenacity import (
|
||||
wait_exponential,
|
||||
retry_if_exception_type,
|
||||
)
|
||||
from lightrag.utils import (
|
||||
from minirag.utils import (
|
||||
wrap_embedding_func_with_attrs,
|
||||
locate_json_string_body_from_string,
|
||||
safe_unicode_decode,
|
||||
@@ -140,7 +140,13 @@ async def openai_complete_if_cache(
|
||||
|
||||
return inner()
|
||||
else:
|
||||
if not response or not hasattr(response, "choices") or not response.choices:
|
||||
logger.error("No valid choices returned. Full response: %s", response)
|
||||
return "" # or raise a more specific exception
|
||||
content = response.choices[0].message.content
|
||||
if content is None:
|
||||
logger.error("The message content is None. Full response: %s", response)
|
||||
return ""
|
||||
if r"\u" in content:
|
||||
content = safe_unicode_decode(content.encode("utf-8"))
|
||||
return content
|
||||
@@ -208,6 +214,30 @@ async def nvidia_openai_complete(
|
||||
return locate_json_string_body_from_string(result)
|
||||
return result
|
||||
|
||||
async def openrouter_openai_complete(
|
||||
prompt,
|
||||
system_prompt=None,
|
||||
history_messages=[],
|
||||
keyword_extraction=False,
|
||||
api_key: str = None,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
if api_key:
|
||||
os.environ["OPENROUTER_API_KEY"] = api_key
|
||||
|
||||
keyword_extraction = kwargs.pop("keyword_extraction", None)
|
||||
result = await openai_complete_if_cache(
|
||||
"google/gemini-2.0-flash-001", # change accordingly
|
||||
prompt,
|
||||
system_prompt=system_prompt,
|
||||
history_messages=history_messages,
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
api_key=api_key,
|
||||
**kwargs,
|
||||
)
|
||||
if keyword_extraction: # TODO: use JSON API
|
||||
return locate_json_string_body_from_string(result)
|
||||
return result
|
||||
|
||||
@wrap_embedding_func_with_attrs(embedding_dim=1536, max_token_size=8192)
|
||||
@retry(
|
||||
|
||||
@@ -254,6 +254,21 @@ def xml_to_json(xml_file):
|
||||
print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
def safe_unicode_decode(content):
|
||||
# Regular expression to find all Unicode escape sequences of the form \uXXXX
|
||||
unicode_escape_pattern = re.compile(r"\\u([0-9a-fA-F]{4})")
|
||||
|
||||
# Function to replace the Unicode escape with the actual character
|
||||
def replace_unicode_escape(match):
|
||||
# Convert the matched hexadecimal value into the actual Unicode character
|
||||
return chr(int(match.group(1), 16))
|
||||
|
||||
# Perform the substitution
|
||||
decoded_content = unicode_escape_pattern.sub(
|
||||
replace_unicode_escape, content.decode("utf-8")
|
||||
)
|
||||
|
||||
return decoded_content
|
||||
|
||||
def process_combine_contexts(hl, ll):
|
||||
header = None
|
||||
@@ -360,7 +375,6 @@ def cal_path_score_list(candidate_reasoning_path, maybe_answer_list):
|
||||
scored_reasoning_path[k] = {"Score": score, "Path": scores}
|
||||
return scored_reasoning_path
|
||||
|
||||
|
||||
def edge_vote_path(path_dict, edge_list):
|
||||
return_dict = copy.deepcopy(path_dict)
|
||||
EDGELIST = []
|
||||
|
||||
Reference in New Issue
Block a user