File size: 1,440 Bytes
cad351c
 
 
 
 
 
 
 
 
ff3e04b
 
 
 
cad351c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)