Skip to content

Commit

Permalink
Merge pull request #1032 from winsonluk/strategic-limit
Browse files Browse the repository at this point in the history
Introduce strategic_token_limit to fix Anthropic bug
  • Loading branch information
assafelovic authored Dec 21, 2024
2 parents 04fed82 + aa8e890 commit d37418a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/docs/gpt-researcher/gptr/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Below is a list of current supported options:
- **`CURATE_SOURCES`**: Whether to curate sources for research. This step adds an LLM run which may increase costs and total run time but improves quality of source selection. Defaults to `True`.
- **`FAST_TOKEN_LIMIT`**: Maximum token limit for fast LLM responses. Defaults to `2000`.
- **`SMART_TOKEN_LIMIT`**: Maximum token limit for smart LLM responses. Defaults to `4000`.
- **`STRATEGIC_TOKEN_LIMIT`**: Maximum token limit for strategic LLM responses. Defaults to `4000`.
- **`BROWSE_CHUNK_MAX_LENGTH`**: Maximum length of text chunks to browse in web sources. Defaults to `8192`.
- **`SUMMARY_TOKEN_LIMIT`**: Maximum token limit for generating summaries. Defaults to `700`.
- **`TEMPERATURE`**: Sampling temperature for LLM responses, typically between 0 and 1. A higher value results in more randomness and creativity, while a lower value results in more focused and deterministic responses. Defaults to `0.55`.
Expand Down
35 changes: 25 additions & 10 deletions gpt_researcher/actions/query_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,31 @@ async def generate_sub_queries(
cost_callback=cost_callback,
)
except Exception as e:
logger.warning(f"Error with strategic LLM: {e}. Falling back to smart LLM.")
response = await create_chat_completion(
model=cfg.smart_llm_model,
messages=[{"role": "user", "content": gen_queries_prompt}],
temperature=cfg.temperature,
max_tokens=cfg.smart_token_limit,
llm_provider=cfg.smart_llm_provider,
llm_kwargs=cfg.llm_kwargs,
cost_callback=cost_callback,
)
logger.warning(f"Error with strategic LLM: {e}. Retrying with max_tokens={cfg.strategic_token_limit}.")
logger.warning(f"See https://github.com/assafelovic/gpt-researcher/issues/1022")
try:
response = await create_chat_completion(
model=cfg.strategic_llm_model,
messages=[{"role": "user", "content": gen_queries_prompt}],
temperature=1,
llm_provider=cfg.strategic_llm_provider,
max_tokens=cfg.strategic_token_limit,
llm_kwargs=cfg.llm_kwargs,
cost_callback=cost_callback,
)
logger.warning(f"Retrying with max_tokens={cfg.strategic_token_limit} successful.")
except Exception as e:
logger.warning(f"Retrying with max_tokens={cfg.strategic_token_limit} failed.")
logger.warning(f"Error with strategic LLM: {e}. Falling back to smart LLM.")
response = await create_chat_completion(
model=cfg.smart_llm_model,
messages=[{"role": "user", "content": gen_queries_prompt}],
temperature=cfg.temperature,
max_tokens=cfg.smart_token_limit,
llm_provider=cfg.smart_llm_provider,
llm_kwargs=cfg.llm_kwargs,
cost_callback=cost_callback,
)

return json_repair.loads(response)

Expand Down
1 change: 1 addition & 0 deletions gpt_researcher/config/variables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BaseConfig(TypedDict):
STRATEGIC_LLM: str
FAST_TOKEN_LIMIT: int
SMART_TOKEN_LIMIT: int
STRATEGIC_TOKEN_LIMIT: int
BROWSE_CHUNK_MAX_LENGTH: int
SUMMARY_TOKEN_LIMIT: int
TEMPERATURE: float
Expand Down
1 change: 1 addition & 0 deletions gpt_researcher/config/variables/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"STRATEGIC_LLM": "openai:gpt-4o", # Can be used with gpt-o1
"FAST_TOKEN_LIMIT": 2000,
"SMART_TOKEN_LIMIT": 4000,
"STRATEGIC_TOKEN_LIMIT": 4000,
"BROWSE_CHUNK_MAX_LENGTH": 8192,
"CURATE_SOURCES": False,
"SUMMARY_TOKEN_LIMIT": 700,
Expand Down

0 comments on commit d37418a

Please sign in to comment.