nathanrchn / perplexityai

A python api to use perplexity.ai
233 stars 60 forks source link

Answer makes no sense #35

Open George3d6 opened 1 year ago

George3d6 commented 1 year ago

I execute the following code:

    perplexity = Perplexity("my@e.mail")
    answer = perplexity.search(q)
    for a in answer:
        wr = a["web_results"]
        print(wr[0]["snippet"])

The answer seems to have 75 yields but they are all the exact same sentence (as far of the snippet for the web result) -- it never gets completed.

Same happens without login

suckerfish commented 10 months ago

seems the responses can be chunked. I had chatgpt help me with code to deal with chunked responses. Not sure if that is exactly what's happening with you but it helped me with incomplete or duplicate looking results. Note that I'm using 'answer' and not 'snippet' though:

`answer = perplexity.search(text)

full_answer = ""
last_chunk = ""
for item in answer:
    if 'answer' in item and item['answer']:
        current_chunk = item['answer']
        if current_chunk.startswith(last_chunk):
            new_content = current_chunk[len(last_chunk):]
            full_answer += new_content
        else:
            full_answer += current_chunk
        last_chunk = current_chunk

        if 'final' in item and item['final']:
            break

if full_answer:
    print(textwrap.fill(full_answer, width=90))
else:
    logging.error("No valid results found")`