Skip to content

armgit / storm

Public Imported from GitHub
0

An LLM-powered knowledge curation system that researches a topic and generates a full-length report with citations.

Support Gemini model.

shaoyijia34d38dea3a16
diff --git a/examples/run_storm_wiki_gemini.py b/examples/run_storm_wiki_gemini.py
new file mode 100644
index 0000000..f1f1d2e
--- /dev/null
+++ b/examples/run_storm_wiki_gemini.py
@@ -0,0 +1,119 @@
+"""
+STORM Wiki pipeline powered by Google Gemini models and search engine.
+You need to set up the following environment variables to run this script:
+ - GOOGLE_API_KEY: Google API key (Can be obtained from https://ai.google.dev/gemini-api/docs/api-key)
+ - YDC_API_KEY: You.com API key; or, BING_SEARCH_API_KEY: Bing Search API key
+
+Output will be structured as below
+args.output_dir/
+ topic_name/ # topic_name will follow convention of underscore-connected topic name w/o space and slash
+ conversation_log.json # Log of information-seeking conversation
+ raw_search_results.json # Raw search results from search engine
+ direct_gen_outline.txt # Outline directly generated with LLM's parametric knowledge
+ storm_gen_outline.txt # Outline refined with collected information
+ url_to_info.json # Sources that are used in the final article
+ storm_gen_article.txt # Final article generated
+ storm_gen_article_polished.txt # Polished final article (if args.do_polish_article is True)
+"""
+
+import os
+from argparse import ArgumentParser
+
+from knowledge_storm import STORMWikiRunnerArguments, STORMWikiRunner, STORMWikiLMConfigs
+from knowledge_storm.lm import GoogleModel
+from knowledge_storm.rm import YouRM, BingSearch
+from knowledge_storm.utils import load_api_key
+
+
+def main(args):
+ load_api_key(toml_file_path='secrets.toml')
+ lm_configs = STORMWikiLMConfigs()
+ gemini_kwargs = {
+ 'api_key': os.getenv("GOOGLE_API_KEY"),
+ 'temperature': 1.0,
+ 'top_p': 0.9
+ }
+
+ # STORM is a LM system so different components can be powered by different models.
+ # For a good balance between cost and quality, you can choose a cheaper/faster model for conv_simulator_lm
+ # which is used to split queries, synthesize answers in the conversation. We recommend using stronger models
+ # for outline_gen_lm which is responsible for organizing the collected information, and article_gen_lm
+ # which is responsible for generating sections with citations.
+ # To check out available Google models, see:
+ # https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=python#list_models
+ conv_simulator_lm = GoogleModel(model='models/gemini-1.5-flash', max_tokens=500, **gemini_kwargs)
+ question_asker_lm = GoogleModel(model='models/gemini-1.5-flash', max_tokens=500, **gemini_kwargs)
+ outline_gen_lm = GoogleModel(model='models/gemini-1.5-pro', max_tokens=400, **gemini_kwargs)
+ article_gen_lm = GoogleModel(model='models/gemini-1.5-pro', max_tokens=700, **gemini_kwargs)
+ article_polish_lm = GoogleModel(model='models/gemini-1.5-pro', max_tokens=4000, **gemini_kwargs)
+
+ lm_configs.set_conv_simulator_lm(conv_simulator_lm)
+ lm_configs.set_question_asker_lm(question_asker_lm)
+ lm_configs.set_outline_gen_lm(outline_gen_lm)
+ lm_configs.set_article_gen_lm(article_gen_lm)
+ lm_configs.set_article_polish_lm(article_polish_lm)
+
+ engine_args = STORMWikiRunnerArguments(
+ output_dir=args.output_dir,
+ max_conv_turn=args.max_conv_turn,
+ max_perspective=args.max_perspective,
+ search_top_k=args.search_top_k,
+ max_thread_num=args.max_thread_num,
+ )
+
+ # STORM is a knowledge curation system which consumes information from the retrieval module.
+ # Currently, the information source is the Internet and we use search engine API as the retrieval module.
+ if args.retriever == 'bing':
+ rm = BingSearch(bing_search_api=os.getenv('BING_SEARCH_API_KEY'), k=engine_args.search_top_k)
+ elif args.retriever == 'you':
+ rm = YouRM(ydc_api_key=os.getenv('YDC_API_KEY'), k=engine_args.search_top_k)
+
+ runner = STORMWikiRunner(engine_args, lm_configs, rm)
+
+ topic = input('Topic: ')
+ runner.run(
+ topic=topic,
+ do_research=args.do_research,
+ do_generate_outline=args.do_generate_outline,
+ do_generate_article=args.do_generate_article,
+ do_polish_article=args.do_polish_article,
+ )
+ runner.post_run()
+ runner.summary()
+
+
+if __name__ == '__main__':
+ parser = ArgumentParser()
+ # global arguments
+ parser.add_argument('--output-dir', type=str, default='./results/gemini',
+ help='Directory to store the outputs.')
+ parser.add_argument('--max-thread-num', type=int, default=3,
+ help='Maximum number of threads to use. The information seeking part and the article generation'
+ 'part can speed up by using multiple threads. Consider reducing it if keep getting '
+ '"Exceed rate limit" error when calling LM API.')
+ parser.add_argument('--retriever', type=str, choices=['bing', 'you'],
+ help='The search engine API to use for retrieving information.')
+ # stage of the pipeline
+ parser.add_argument('--do-research', action='store_true',
+ help='If True, simulate conversation to research the topic; otherwise, load the results.')
+ parser.add_argument('--do-generate-outline', action='store_true',
+ help='If True, generate an outline for the topic; otherwise, load the results.')
+ parser.add_argument('--do-generate-article', action='store_true',
+ help='If True, generate an article for the topic; otherwise, load the results.')
+ parser.add_argument('--do-polish-article', action='store_true',
+ help='If True, polish the article by adding a summarization section and (optionally) removing '
+ 'duplicate content.')
+ # hyperparameters for the pre-writing stage
+ parser.add_argument('--max-conv-turn', type=int, default=3,
+ help='Maximum number of questions in conversational question asking.')
+ parser.add_argument('--max-perspective', type=int, default=3,
+ help='Maximum number of perspectives to consider in perspective-guided question asking.')
+ parser.add_argument('--search-top-k', type=int, default=3,
+ help='Top k search results to consider for each search query.')
+ # hyperparameters for the writing stage
+ parser.add_argument('--retrieve-top-k', type=int, default=3,
+ help='Top k collected references for each section title.')
+ parser.add_argument('--remove-duplicate', action='store_true',
+ help='If True, remove duplicate content from the article.')
+
+ main(parser.parse_args())
diff --git a/knowledge_storm/lm.py b/knowledge_storm/lm.py
index e9c5085..79d9ba5 100644
--- a/knowledge_storm/lm.py
+++ b/knowledge_storm/lm.py
@@ -196,6 +196,7 @@ class DeepSeekModel(dspy.OpenAI):
class AzureOpenAIModel(dspy.AzureOpenAI):
"""A wrapper class for dspy.AzureOpenAI."""
+
def __init__(
self,
api_base: Optional[str] = None,
@@ -413,7 +414,7 @@ class OllamaClient(dspy.OllamaLocal):
super().__init__(model=model, base_url=f"{url}:{port}", **kwargs)
# Store additional kwargs for the generate method.
self.kwargs = {**self.kwargs, **kwargs}
-
+
class TGIClient(dspy.HFClientTGI):
def __init__(self, model, port, url, http_request_kwargs=None, **kwargs):
@@ -593,3 +594,123 @@ class TogetherClient(dspy.HFModel):
completions = [resp_json.get('choices', [])[0].get('text', "")]
response = {"prompt": prompt, "choices": [{"text": c} for c in completions]}
return response
+
+
+class GoogleModel(dspy.dsp.modules.lm.LM):
+ """A wrapper class for Google Gemini API."""
+
+ def __init__(
+ self,
+ model: str,
+ api_key: Optional[str] = None,
+ **kwargs,
+ ):
+ """You can use `genai.list_models()` to get a list of available models."""
+ super().__init__(model)
+ try:
+ import google.generativeai as genai
+ except ImportError as err:
+ raise ImportError("GoogleModel requires `pip install google-generativeai`.") from err
+
+ api_key = os.environ.get("GOOGLE_API_KEY") if api_key is None else api_key
+ genai.configure(api_key=api_key)
+
+ kwargs = {
+ "candidate_count": 1, # Caveat: Gemini API supports only one candidate for now.
+ "temperature": 0.0 if "temperature" not in kwargs else kwargs["temperature"],
+ "max_output_tokens": kwargs["max_tokens"],
+ "top_p": 1,
+ "top_k": 1,
+ **kwargs,
+ }
+
+ kwargs.pop("max_tokens", None) # GenerationConfig cannot accept max_tokens
+
+ self.model = model
+ self.config = genai.GenerationConfig(**kwargs)
+ self.llm = genai.GenerativeModel(model_name=model,
+ generation_config=self.config)
+
+ self.kwargs = {
+ "n": 1,
+ **kwargs,
+ }
+
+ self.history: list[dict[str, Any]] = []
+
+ self._token_usage_lock = threading.Lock()
+ self.prompt_tokens = 0
+ self.completion_tokens = 0
+
+ def log_usage(self, response):
+ """Log the total tokens from the Google API response."""
+ usage_data = response.usage_metadata
+ if usage_data:
+ with self._token_usage_lock:
+ self.prompt_tokens += usage_data.prompt_token_count
+ self.completion_tokens += usage_data.candidates_token_count
+
+ def get_usage_and_reset(self):
+ """Get the total tokens used and reset the token usage."""
+ usage = {
+ self.model:
+ {'prompt_tokens': self.prompt_tokens, 'completion_tokens': self.completion_tokens}
+ }
+ self.prompt_tokens = 0
+ self.completion_tokens = 0
+
+ return usage
+
+ def basic_request(self, prompt: str, **kwargs):
+ raw_kwargs = kwargs
+ kwargs = {
+ **self.kwargs,
+ **kwargs,
+ }
+
+ # Google disallows "n" arguments.
+ n = kwargs.pop("n", None)
+
+ response = self.llm.generate_content(prompt, generation_config=kwargs)
+
+ history = {
+ "prompt": prompt,
+ "response": [response.to_dict()],
+ "kwargs": kwargs,
+ "raw_kwargs": raw_kwargs,
+ }
+ self.history.append(history)
+
+ return response
+
+ @backoff.on_exception(
+ backoff.expo,
+ (Exception,),
+ max_time=1000,
+ max_tries=8,
+ on_backoff=backoff_hdlr,
+ giveup=giveup_hdlr,
+ )
+ def request(self, prompt: str, **kwargs):
+ """Handles retrieval of completions from Google whilst handling API errors"""
+ return self.basic_request(prompt, **kwargs)
+
+ def __call__(
+ self,
+ prompt: str,
+ only_completed: bool = True,
+ return_sorted: bool = False,
+ **kwargs,
+ ):
+ assert only_completed, "for now"
+ assert return_sorted is False, "for now"
+
+ n = kwargs.pop("n", 1)
+
+ completions = []
+ for _ in range(n):
+ response = self.request(prompt, **kwargs)
+ self.log_usage(response)
+ completions.append(response.parts[0].text)
+
+ return completions