| 1 | |
| 2 | from openai import OpenAI |
| 3 | import models |
| 4 | |
| 5 | def perplexity_search(query:str, model_name="llama-3.1-sonar-large-128k-online",api_key=None,base_url="https://api.perplexity.ai"): |
| 6 | api_key = api_key or models.get_api_key("perplexity") |
| 7 | |
| 8 | client = OpenAI(api_key=api_key, base_url=base_url) |
| 9 | |
| 10 | messages = [ |
| 11 | #It is recommended to use only single-turn conversations and avoid system prompts for the online LLMs (sonar-small-online and sonar-medium-online). |
| 12 | |
| 13 | # { |
| 14 | # "role": "system", |
| 15 | # "content": ( |
| 16 | # "You are an artificial intelligence assistant and you need to " |
| 17 | # "engage in a helpful, detailed, polite conversation with a user." |
| 18 | # ), |
| 19 | # }, |
| 20 | { |
| 21 | "role": "user", |
| 22 | "content": ( |
| 23 | query |
| 24 | ), |
| 25 | }, |
| 26 | ] |
| 27 | |
| 28 | response = client.chat.completions.create( |
| 29 | model=model_name, |
| 30 | messages=messages, # type: ignore |
| 31 | ) |
| 32 | result = response.choices[0].message.content #only the text is returned |
| 33 | return result |