import requests import json from dotenv import load_dotenv import os load_dotenv() ALLOFRESH_SEARCH_API_BASE = os.getenv("ALLOFRESH_SEARCH_API_BASE") def search_allo_api(query, limit=3): response = requests.get( f'{ALLOFRESH_SEARCH_API_BASE}?keyword={query}&limit={limit}&p=1', timeout=120 ) return json.loads(response.text) def lctool_search_allo_api(queries): all_results = [] try: for q in queries.split(", "): prods_list = search_allo_api(q)["products"] all_results.append({ q: [ {k: v for k, v in prod_dict.items() if k in ["name", "price"]} for prod_dict in prods_list ] }) return str(all_results) except Exception as e: return str(e) def cut_dialogue_history(history_memory, keep_last_n_words=500): if history_memory is None or len(history_memory) == 0: return history_memory tokens = history_memory.split() n_tokens = len(tokens) # print(f"history_memory: {history_memory}, n_tokens: {n_tokens}") if n_tokens < keep_last_n_words: return history_memory paragraphs = history_memory.split('\n') last_n_tokens = n_tokens while last_n_tokens >= keep_last_n_words: last_n_tokens -= len(paragraphs[0].split(' ')) paragraphs = paragraphs[1:] return '\n' + '\n'.join(paragraphs)