ScrapeGraphAI / Scrapegraph-ai

Python scraper based on AI
https://scrapegraphai.com
MIT License
15.87k stars 1.29k forks source link

Can not Set Model Tokens to Local Model with OpenAI API Format #810

Open fahadh4ilyas opened 3 days ago

fahadh4ilyas commented 3 days ago

Describe the bug I have my own local model loaded using vLLM. So, it's using openAI API format. When I tried it to SmartScraperGraph, I got the error: TypeError: Completions.create() got an unexpected keyword argument 'model_tokens'

To Reproduce

import json
from scrapegraphai.graphs import SmartScraperGraph

# Define the configuration for the scraping pipeline
graph_config = {
    "llm": {
        "api_key": "sk-abc",
        "model": "openai/Qwen2.5-7B",
        "base_url": "http://127.0.0.1:8000/v1",
        "model_token": 32768
    },
    "verbose": True,
    "headless": False,
}

# Create the SmartScraperGraph instance
smart_scraper_graph = SmartScraperGraph(
    prompt="Find some information about what does the company do, the name and a contact email.",
    source="https://scrapegraphai.com/",
    config=graph_config
)

# Run the pipeline
result = smart_scraper_graph.run()
print(json.dumps(result, indent=4))

Here is the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 27
     20 smart_scraper_graph = SmartScraperGraph(
     21     prompt="Find some information about what does the company do, the name and a contact email.",
     22     source="https://scrapegraphai.com/",
     23     config=graph_config
     24 )
     26 # Run the pipeline
---> 27 result = smart_scraper_graph.run()
     28 print(json.dumps(result, indent=4))

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/scrapegraphai/graphs/smart_scraper_graph.py:212, in SmartScraperGraph.run(self)
    204 """
    205 Executes the scraping process and returns the answer to the prompt.
    206 
    207 Returns:
    208     str: The answer to the prompt.
    209 """
    211 inputs = {"user_prompt": self.prompt, self.input_key: self.source}
--> 212 self.final_state, self.execution_info = self.graph.execute(inputs)
    214 return self.final_state.get("answer", "No answer found.")

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/scrapegraphai/graphs/base_graph.py:327, in BaseGraph.execute(self, initial_state)
    325     return (result["_state"], [])
    326 else:
--> 327     return self._execute_standard(initial_state)

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/scrapegraphai/graphs/base_graph.py:274, in BaseGraph._execute_standard(self, initial_state)
    261         graph_execution_time = time.time() - start_time
    262         log_graph_execution(
    263             graph_name=self.graph_name,
    264             source=source,
   (...)
    272             exception=str(e)
    273         )
--> 274         raise e
    276 # Add total results to execution info
    277 exec_info.append({
    278     "node_name": "TOTAL RESULT",
    279     "total_tokens": cb_total["total_tokens"],
   (...)
    284     "exec_time": total_exec_time,
    285 })

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/scrapegraphai/graphs/base_graph.py:247, in BaseGraph._execute_standard(self, initial_state)
    244     schema = self._get_schema(current_node)
    246 try:
--> 247     result, node_exec_time, cb_data = self._execute_node(
    248         current_node, state, llm_model, llm_model_name
    249     )
    250     total_exec_time += node_exec_time
    252     if cb_data:

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/scrapegraphai/graphs/base_graph.py:172, in BaseGraph._execute_node(self, current_node, state, llm_model, llm_model_name)
    169 curr_time = time.time()
    171 with self.callback_manager.exclusive_get_callback(llm_model, llm_model_name) as cb:
--> 172     result = current_node.execute(state)
    173     node_exec_time = time.time() - curr_time
    175     cb_data = None

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/scrapegraphai/nodes/generate_answer_node.py:128, in GenerateAnswerNode.execute(self, state)
    125 if output_parser:
    126     chain = chain | output_parser
--> 128 answer = chain.invoke({"question": user_prompt})
    129 state.update({self.output[0]: answer})
    130 return state

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_core/runnables/base.py:3024, in RunnableSequence.invoke(self, input, config, **kwargs)
   3022             input = context.run(step.invoke, input, config, **kwargs)
   3023         else:
-> 3024             input = context.run(step.invoke, input, config)
   3025 # finish the root run
   3026 except BaseException as e:

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:286, in BaseChatModel.invoke(self, input, config, stop, **kwargs)
    275 def invoke(
    276     self,
    277     input: LanguageModelInput,
   (...)
    281     **kwargs: Any,
    282 ) -> BaseMessage:
    283     config = ensure_config(config)
    284     return cast(
    285         ChatGeneration,
--> 286         self.generate_prompt(
    287             [self._convert_input(input)],
    288             stop=stop,
    289             callbacks=config.get("callbacks"),
    290             tags=config.get("tags"),
    291             metadata=config.get("metadata"),
    292             run_name=config.get("run_name"),
    293             run_id=config.pop("run_id", None),
    294             **kwargs,
    295         ).generations[0][0],
    296     ).message

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:786, in BaseChatModel.generate_prompt(self, prompts, stop, callbacks, **kwargs)
    778 def generate_prompt(
    779     self,
    780     prompts: list[PromptValue],
   (...)
    783     **kwargs: Any,
    784 ) -> LLMResult:
    785     prompt_messages = [p.to_messages() for p in prompts]
--> 786     return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:643, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)
    641         if run_managers:
    642             run_managers[i].on_llm_error(e, response=LLMResult(generations=[]))
--> 643         raise e
    644 flattened_outputs = [
    645     LLMResult(generations=[res.generations], llm_output=res.llm_output)  # type: ignore[list-item]
    646     for res in results
    647 ]
    648 llm_output = self._combine_llm_outputs([res.llm_output for res in results])

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:633, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)
    630 for i, m in enumerate(messages):
    631     try:
    632         results.append(
--> 633             self._generate_with_cache(
    634                 m,
    635                 stop=stop,
    636                 run_manager=run_managers[i] if run_managers else None,
    637                 **kwargs,
    638             )
    639         )
    640     except BaseException as e:
    641         if run_managers:

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py:851, in BaseChatModel._generate_with_cache(self, messages, stop, run_manager, **kwargs)
    849 else:
    850     if inspect.signature(self._generate).parameters.get("run_manager"):
--> 851         result = self._generate(
    852             messages, stop=stop, run_manager=run_manager, **kwargs
    853         )
    854     else:
    855         result = self._generate(messages, stop=stop, **kwargs)

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/langchain_openai/chat_models/base.py:689, in BaseChatOpenAI._generate(self, messages, stop, run_manager, **kwargs)
    687     generation_info = {"headers": dict(raw_response.headers)}
    688 else:
--> 689     response = self.client.create(**payload)
    690 return self._create_chat_result(response, generation_info)

File ~/anaconda3/envs/simple-reasoning/lib/python3.11/site-packages/openai/_utils/_utils.py:275, in required_args.<locals>.inner.<locals>.wrapper(*args, **kwargs)
    273             msg = f"Missing required argument: {quote(missing[0])}"
    274     raise TypeError(msg)
--> 275 return func(*args, **kwargs)

TypeError: Completions.create() got an unexpected keyword argument 'model_token'

Expected behavior The result return as expected

VinciGit00 commented 3 days ago

Is qwen from OpenAI?

fahadh4ilyas commented 3 days ago

Is qwen from OpenAI?

Nope, but if I have local model loaded with vLLM which is using OpenAI format, is that how I load it right? I am refering to #721