Skip to content

Instantly share code, notes, and snippets.

@hqman
Last active February 17, 2025 03:08
Show Gist options
  • Save hqman/37dad0dbd1b2161d31734c02a8c952b5 to your computer and use it in GitHub Desktop.
Save hqman/37dad0dbd1b2161d31734c02a8c952b5 to your computer and use it in GitHub Desktop.
deepseek_search.py
from duckduckgo_search import DDGS
from typing import List, Dict
import openai
from typing import Optional
from datetime import datetime
client = openai.OpenAI(
api_key="sk-xxxxx",
base_url="https://api.lkeap.cloud.tencent.com/v1",
)
# Search result template
search_answer_en_template = """# The following contents are the search results related to the user's message:
{search_results}
In the search results I provide to you, each result is formatted as [webpage X begin]...[webpage X end], where X represents the numerical index of each article. Please cite the context at the end of the relevant sentence when appropriate. Use the citation format [citation:X] in the corresponding part of your answer. If a sentence is derived from multiple contexts, list all relevant citation numbers, such as [citation:3][citation:5]. Be sure not to cluster all citations at the end; instead, include them in the corresponding parts of the answer.
When responding, please keep the following points in mind:
- Today is {cur_date}.
- Not all content in the search results is closely related to the user's question. You need to evaluate and filter the search results based on the question.
- For listing-type questions (e.g., listing all flight information), try to limit the answer to 10 key points and inform the user that they can refer to the search sources for complete information. Prioritize providing the most complete and relevant items in the list. Avoid mentioning content not provided in the search results unless necessary.
- For creative tasks (e.g., writing an essay), ensure that references are cited within the body of the text, such as [citation:3][citation:5], rather than only at the end of the text. You need to interpret and summarize the user's requirements, choose an appropriate format, fully utilize the search results, extract key information, and generate an answer that is insightful, creative, and professional. Extend the length of your response as much as possible, addressing each point in detail and from multiple perspectives, ensuring the content is rich and thorough.
- If the response is lengthy, structure it well and summarize it in paragraphs. If a point-by-point format is needed, try to limit it to 5 points and merge related content.
- For objective Q&A, if the answer is very brief, you may add one or two related sentences to enrich the content.
- Choose an appropriate and visually appealing format for your response based on the user's requirements and the content of the answer, ensuring strong readability.
- Your answer should synthesize information from multiple relevant webpages and avoid repeatedly citing the same webpage.
- Unless the user requests otherwise, your response should be in the same language as the user's question.
# The user's message is:
{question}"""
def format_search_results(results: List[Dict]) -> str:
"""
Format search results into the template required format
"""
formatted_results = []
for i, result in enumerate(results, 1):
webpage = f"[webpage {i} begin]\n"
webpage += f"title: {result.get('title', '')}\n"
webpage += f"content: {result.get('body', '')}\n"
webpage += f"[webpage {i} end]"
formatted_results.append(webpage)
return "\n\n".join(formatted_results)
def chat_query(
prompt: str, search_results: List[Dict], model: str = "deepseek-r1"
) -> Optional[str]:
"""
Use OpenAI API for chat queries combined with search results
Args:
prompt (str): User input question
search_results (List[Dict]): Search results
model (str): Model name to use
Returns:
Optional[str]: AI response, None if error occurs
"""
try:
# Format search results
formatted_results = format_search_results(search_results)
# Get current date
current_date = datetime.now().strftime("%Y-%m-%d")
# Build complete prompt
full_prompt = search_answer_en_template.format(
search_results=formatted_results, cur_date=current_date, question=prompt
)
# Call API
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": full_prompt}],
temperature=0.6,
max_tokens=5000,
)
return response.choices[0].message.content
except Exception as e:
print(f"OpenAI API call error: {str(e)}")
return None
def search_query(query: str, max_results: int = 5) -> List[Dict]:
"""
Use DuckDuckGo to search and return results
Args:
query (str): Search query term
max_results (int): Maximum number of results to return, default is 5
Returns:
List[Dict]: List of search results, each containing title, link, body etc.
"""
try:
with DDGS() as ddgs:
results = list(ddgs.text(query, max_results=max_results))
return results
except Exception as e:
print(f"Search error: {str(e)}")
return []
def main():
# Usage example
question = "Who scored the goal for Brighton in today's match?"
# First search in English
print("=== Search related information ===")
search_results = search_query(question, 50)
# Use AI to answer questions
print("\n=== AI answer ===")
response = chat_query(question, search_results)
if response:
print(f"\nQuestion: {question}")
print(f"AI answer:\n{response}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment