Spaces:
Running
Running
File size: 8,442 Bytes
372531f |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
import asyncio
from typing import List, Dict, Any
from ..config.config import Config
from ..utils.llm import create_chat_completion
from ..utils.logger import get_formatted_logger
from ..prompts import (
generate_report_introduction,
generate_draft_titles_prompt,
generate_report_conclusion,
get_prompt_by_report_type,
)
from ..utils.enum import Tone
logger = get_formatted_logger()
async def write_report_introduction(
query: str,
context: str,
agent_role_prompt: str,
config: Config,
websocket=None,
cost_callback: callable = None
) -> str:
"""
Generate an introduction for the report.
Args:
query (str): The research query.
context (str): Context for the report.
role (str): The role of the agent.
config (Config): Configuration object.
websocket: WebSocket connection for streaming output.
cost_callback (callable, optional): Callback for calculating LLM costs.
Returns:
str: The generated introduction.
"""
try:
introduction = await create_chat_completion(
model=config.smart_llm_model,
messages=[
{"role": "system", "content": f"{agent_role_prompt}"},
{"role": "user", "content": generate_report_introduction(
query, context)},
],
temperature=0.25,
llm_provider=config.smart_llm_provider,
stream=True,
websocket=websocket,
max_tokens=config.smart_token_limit,
llm_kwargs=config.llm_kwargs,
cost_callback=cost_callback,
)
return introduction
except Exception as e:
logger.error(f"Error in generating report introduction: {e}")
return ""
async def write_conclusion(
query: str,
context: str,
agent_role_prompt: str,
config: Config,
websocket=None,
cost_callback: callable = None
) -> str:
"""
Write a conclusion for the report.
Args:
query (str): The research query.
context (str): Context for the report.
role (str): The role of the agent.
config (Config): Configuration object.
websocket: WebSocket connection for streaming output.
cost_callback (callable, optional): Callback for calculating LLM costs.
Returns:
str: The generated conclusion.
"""
try:
conclusion = await create_chat_completion(
model=config.smart_llm_model,
messages=[
{"role": "system", "content": f"{agent_role_prompt}"},
{"role": "user", "content": generate_report_conclusion(query, context)},
],
temperature=0.25,
llm_provider=config.smart_llm_provider,
stream=True,
websocket=websocket,
max_tokens=config.smart_token_limit,
llm_kwargs=config.llm_kwargs,
cost_callback=cost_callback,
)
return conclusion
except Exception as e:
logger.error(f"Error in writing conclusion: {e}")
return ""
async def summarize_url(
url: str,
content: str,
role: str,
config: Config,
websocket=None,
cost_callback: callable = None
) -> str:
"""
Summarize the content of a URL.
Args:
url (str): The URL to summarize.
content (str): The content of the URL.
role (str): The role of the agent.
config (Config): Configuration object.
websocket: WebSocket connection for streaming output.
cost_callback (callable, optional): Callback for calculating LLM costs.
Returns:
str: The summarized content.
"""
try:
summary = await create_chat_completion(
model=config.smart_llm_model,
messages=[
{"role": "system", "content": f"{role}"},
{"role": "user", "content": f"Summarize the following content from {url}:\n\n{content}"},
],
temperature=0.25,
llm_provider=config.smart_llm_provider,
stream=True,
websocket=websocket,
max_tokens=config.smart_token_limit,
llm_kwargs=config.llm_kwargs,
cost_callback=cost_callback,
)
return summary
except Exception as e:
logger.error(f"Error in summarizing URL: {e}")
return ""
async def generate_draft_section_titles(
query: str,
current_subtopic: str,
context: str,
role: str,
config: Config,
websocket=None,
cost_callback: callable = None
) -> List[str]:
"""
Generate draft section titles for the report.
Args:
query (str): The research query.
context (str): Context for the report.
role (str): The role of the agent.
config (Config): Configuration object.
websocket: WebSocket connection for streaming output.
cost_callback (callable, optional): Callback for calculating LLM costs.
Returns:
List[str]: A list of generated section titles.
"""
try:
section_titles = await create_chat_completion(
model=config.smart_llm_model,
messages=[
{"role": "system", "content": f"{role}"},
{"role": "user", "content": generate_draft_titles_prompt(
current_subtopic, query, context)},
],
temperature=0.25,
llm_provider=config.smart_llm_provider,
stream=True,
websocket=None,
max_tokens=config.smart_token_limit,
llm_kwargs=config.llm_kwargs,
cost_callback=cost_callback,
)
return section_titles.split("\n")
except Exception as e:
logger.error(f"Error in generating draft section titles: {e}")
return []
async def generate_report(
query: str,
context,
agent_role_prompt: str,
report_type: str,
tone: Tone,
report_source: str,
websocket,
cfg,
main_topic: str = "",
existing_headers: list = [],
relevant_written_contents: list = [],
cost_callback: callable = None,
headers=None,
):
"""
generates the final report
Args:
query:
context:
agent_role_prompt:
report_type:
websocket:
tone:
cfg:
main_topic:
existing_headers:
relevant_written_contents:
cost_callback:
Returns:
report:
"""
generate_prompt = get_prompt_by_report_type(report_type)
report = ""
if report_type == "subtopic_report":
content = f"{generate_prompt(query, existing_headers, relevant_written_contents, main_topic, context, report_format=cfg.report_format, tone=tone, total_words=cfg.total_words, language=cfg.language)}"
else:
content = f"{generate_prompt(query, context, report_source, report_format=cfg.report_format, tone=tone, total_words=cfg.total_words, language=cfg.language)}"
try:
report = await create_chat_completion(
model=cfg.smart_llm_model,
messages=[
{"role": "system", "content": f"{agent_role_prompt}"},
{"role": "user", "content": content},
],
temperature=0.35,
llm_provider=cfg.smart_llm_provider,
stream=True,
websocket=websocket,
max_tokens=cfg.smart_token_limit,
llm_kwargs=cfg.llm_kwargs,
cost_callback=cost_callback,
)
except:
try:
report = await create_chat_completion(
model=cfg.smart_llm_model,
messages=[
{"role": "user", "content": f"{agent_role_prompt}\n\n{content}"},
],
temperature=0.35,
llm_provider=cfg.smart_llm_provider,
stream=True,
websocket=websocket,
max_tokens=cfg.smart_token_limit,
llm_kwargs=cfg.llm_kwargs,
cost_callback=cost_callback,
)
except Exception as e:
print(f"Error in generate_report: {e}")
return report
|