Update app.py
Browse files142 unmodified lines143144145146147148149150151152153146147148149150151152153154155156157158159160161162163164155156157158165166167160161162163168169170171172142 unmodified lines return FAISS.from_documents(documents, embed)
def summarize_article(article, content, query, model, system_prompt, client, temperature=0.2): prompt = f"""Using the following article: Title: {article.get('title', 'No Title Available')} Content (excerpt): {article.get('body', '')[:2000]} # Truncate if too long URL: {article.get('href', 'No URL Available')} And based on the following web search context (excerpt): {content[:2000]} # Truncate if too longdef summarize_article(article, content, model, system_prompt, user_query, client, temperature=0.2): prompt = f"""Summarize the following article in the context of broader web search results:
Article:Title: {article['title']}URL: {article['href']}Content: {article['body'][:500]}... # Truncate to avoid extremely long prompts
Additional Context:{content[:1000]}... # Truncate additional context as well
User Query: {user_query}
Write a detailed and complete research document which addresses the User Query, incorporating both the specific article and the broader context. Focus on the most relevant information."""
# Calculate input tokens (this is an approximation, you might need a more accurate method) input_tokens = len(prompt.split()) // 4 Write a detailed and complete research document. The document should include: 1. An introduction 2. Key findings from both the article and search context 3. A conclusion that directly answers the user's request: '{query}'.""" # Get the token limit for the current model model_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default to 8192 if model not found # Calculate token usage and model limits input_tokens = len(prompt.split()) // 4 # Approximate token count model_token_limit = MODEL_TOKEN_LIMITS.get(model, 8192) # Default limit is 8192 if model is not found max_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap output tokens to avoid exceeding limits # Calculate max_new_tokens max_new_tokens = min(model_token_limit - input_tokens, 6500) # Cap at 6500 to be safe
try: response = client.chat_completion(app.py+22-16