Spaces:
Sleeping
Sleeping
File size: 2,674 Bytes
c493259 6848c8b c493259 ae3c993 c493259 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
from langchain_community.document_loaders import IFixitLoader
from helper_functions.llm_client_initialization import llm_base_client_init
#function for rewriting info into searchphrase
def write_searchphrase(search_info: str, debug: bool = False):
"""
Uses the LLM to rewrite input into a structured searchphrase iFixit searches.
Args:
search_info (str): The information to be turned into a searchphrase.
Returns:
str: The rewritten searchphrase.
"""
client = llm_base_client_init()
chat_completion = client.chat.completions.create(
messages=[
{"role": "system",
"content": """Rewrite the following info into a structured searchphrase for iFixit,
ensuring it includes in this order the device name, model and part that needs to be repaired.
Return only the searchphrase, do not include any other words or comments.
Capitalize the first letter of each word.
The searchphrase should be a single sentence."""},
{"role": "user", "content": search_info},
],
model="llama3-8b-8192",
temperature=0.1
)
search_phrase = chat_completion.choices[0].message.content
if debug == True:
print('Full searchphrase:', search_phrase)
return search_phrase
#load guides from iFixit
def load_guides(search_phrase: str, debug: bool = False):
"""
Load a guide from IFixit based on the search phrase.
If no guide is found, iteratively remove the last word and retry.
Args:
search_phrase (str): The phrase to search for in IFixit guides.
Returns:
guides: The loaded guide data or None if no guide is found.
"""
words = search_phrase.split()
while words:
query = " ".join(words)
guides = IFixitLoader.load_suggestions(query, doc_type='guide')
if guides:
if(debug == True):
print('Used words:', words)
return guides # Return results if found
words.pop() # Remove the last word and retry
print('No guides found')
return None # Return None if no guide is found
def load_ifixit_guides(search_info: str, debug: bool = False):
"""
Rewrites the search info into a searchphrase and loads guides from iFixit.
Args:
search info (str): The information to be turned into a searchphrase.
Returns:
guides: The loaded guide data or None if no guide is found.
"""
search_phrase = write_searchphrase(search_info, debug=debug)
guides = load_guides(search_phrase, debug=debug)
return guides
|