Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Multi Agent PDF saving fix #1008

Closed
kcalhoon opened this issue Dec 6, 2024 · 3 comments · Fixed by #1071
Closed

Multi Agent PDF saving fix #1008

kcalhoon opened this issue Dec 6, 2024 · 3 comments · Fixed by #1071

Comments

@kcalhoon
Copy link

kcalhoon commented Dec 6, 2024

Describe the bug
An error is thrown when trying to save the output to pdf

To Reproduce
Run any query

Expected behavior
A pdf will be saved along with .md and .docx

Solution
In multi_agents/agents/utils/file_formats.py, I modified the css_file_path to:
"css_file_path="./multi_agents/agents/utils/pdf_styles.css""

Code Snippet

`async def write_md_to_pdf(text: str, path: str) -> str:
"""Converts Markdown text to a PDF file and returns the file path.

Args:
    text (str): Markdown text to convert.

Returns:
    str: The encoded file path of the generated PDF.
"""
task = uuid.uuid4().hex
file_path = f"{path}/{task}.pdf"

try:
    # Moved imports to inner function to avoid known import errors with gobject-2.0
    from md2pdf.core import md2pdf
    md2pdf(file_path,
           md_content=text,
           # md_file_path=f"{file_path}.md",
           css_file_path="./multi_agents/agents/utils/pdf_styles.css",  # Updated path 
           base_url=None)
    print(f"Report written to {file_path}")
except Exception as e:
    print(f"Error in converting Markdown to PDF: {e}")
    return ""

encoded_file_path = urllib.parse.quote(file_path)
return encoded_file_path`
@ElishaKay
Copy link
Collaborator

Welcome @kcalhoon

Possible to get this as a PR? We'd love to carve your name into the tree

@PaluriRaviteja
Copy link

Hey hi.. i am trying to run gpt-researcher using google colab but it is throwing the below error. i tried multiple ways. but nothing worked out

/usr/local/lib/python3.10/dist-packages/gpt_researcher/actions/agent_creator.py in choose_agent(query, cfg, parent_query, cost_callback, headers)
26 response = await create_chat_completion(
---> 27 model="gpt-3.5-turbo",
28 messages=[

22 frames
/usr/local/lib/python3.10/dist-packages/gpt_researcher/utils/llm.py in create_chat_completion(messages, model, temperature, max_tokens, llm_provider, stream, websocket, llm_kwargs, cost_callback)
59 for _ in range(10): # maximum of 10 attempts
---> 60 response = await provider.get_chat_response(
61 messages, stream, websocket

/usr/local/lib/python3.10/dist-packages/gpt_researcher/llm_provider/generic/base.py in get_chat_response(self, messages, stream, websocket)
127 # Getting output from the model chain using ainvoke for asynchronous invoking
--> 128 output = await self.llm.ainvoke(messages)
129

/usr/local/lib/python3.10/dist-packages/langchain_core/language_models/chat_models.py in ainvoke(self, input, config, stop, **kwargs)
306 config = ensure_config(config)
--> 307 llm_result = await self.agenerate_prompt(
308 [self._convert_input(input)],

/usr/local/lib/python3.10/dist-packages/langchain_core/language_models/chat_models.py in agenerate_prompt(self, prompts, stop, callbacks, **kwargs)
795 prompt_messages = [p.to_messages() for p in prompts]
--> 796 return await self.agenerate(
797 prompt_messages, stop=stop, callbacks=callbacks, **kwargs

/usr/local/lib/python3.10/dist-packages/langchain_core/language_models/chat_models.py in agenerate(self, messages, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)
755 )
--> 756 raise exceptions[0]
757 flattened_outputs = [

/usr/lib/python3.10/asyncio/tasks.py in __step(failed resolving arguments)
231 # don't have __iter__ and __next__ methods.
--> 232 result = coro.send(None)
233 else:

/usr/local/lib/python3.10/dist-packages/langchain_core/language_models/chat_models.py in _agenerate_with_cache(self, messages, stop, run_manager, **kwargs)
923 if inspect.signature(self._agenerate).parameters.get("run_manager"):
--> 924 result = await self._agenerate(
925 messages, stop=stop, run_manager=run_manager, **kwargs

/usr/local/lib/python3.10/dist-packages/langchain_openai/chat_models/base.py in _agenerate(self, messages, stop, run_manager, **kwargs)
824 else:
--> 825 response = await self.async_client.create(**payload)
826 return await run_in_executor(

/usr/local/lib/python3.10/dist-packages/openai/resources/chat/completions.py in create(self, messages, model, audio, frequency_penalty, function_call, functions, logit_bias, logprobs, max_completion_tokens, max_tokens, metadata, modalities, n, parallel_tool_calls, prediction, presence_penalty, response_format, seed, service_tier, stop, store, stream, stream_options, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)
1660 validate_response_format(response_format)
-> 1661 return await self._post(
1662 "/chat/completions",

/usr/local/lib/python3.10/dist-packages/openai/_base_client.py in post(self, path, cast_to, body, files, options, stream, stream_cls)
1842 )
-> 1843 return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
1844

/usr/local/lib/python3.10/dist-packages/openai/_base_client.py in request(self, cast_to, options, stream, stream_cls, remaining_retries)
1536
-> 1537 return await self._request(
1538 cast_to=cast_to,

/usr/local/lib/python3.10/dist-packages/openai/_base_client.py in _request(self, cast_to, options, stream, stream_cls, retries_taken)
1637 log.debug("Re-raising status error")
-> 1638 raise self._make_status_error_from_response(err.response) from None
1639

NotFoundError: Error code: 404 - {'error': {'message': 'The model gpt-4o-2024-11-20 does not exist or you do not have access to it.', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
in <cell line: 20>()
22 report_type = "research_report"
23
---> 24 report, context, costs, images, sources = asyncio.run(get_report(query, report_type))
25
26 print("Report:")

/usr/local/lib/python3.10/dist-packages/nest_asyncio.py in run(main, debug)
28 task = asyncio.ensure_future(main)
29 try:
---> 30 return loop.run_until_complete(task)
31 finally:
32 if not task.done():

/usr/local/lib/python3.10/dist-packages/nest_asyncio.py in run_until_complete(self, future)
96 raise RuntimeError(
97 'Event loop stopped before Future completed.')
---> 98 return f.result()
99
100 def _run_once(self):

/usr/lib/python3.10/asyncio/futures.py in result(self)
199 self.__log_traceback = False
200 if self._exception is not None:
--> 201 raise self._exception.with_traceback(self._exception_tb)
202 return self._result
203

/usr/lib/python3.10/asyncio/tasks.py in __step(failed resolving arguments)
230 # We use the send method directly, because coroutines
231 # don't have __iter__ and __next__ methods.
--> 232 result = coro.send(None)
233 else:
234 result = coro.throw(exc)

in get_report(query, report_type)
7 async def get_report(query: str, report_type: str) -> str:
8 researcher = GPTResearcher(query, report_type)
----> 9 research_result = await researcher.conduct_research()
10 report = await researcher.write_report()
11

/usr/local/lib/python3.10/dist-packages/gpt_researcher/agent.py in conduct_research(self)
90 async def conduct_research(self):
91 if not (self.agent and self.role):
---> 92 self.agent, self.role = await choose_agent(
93 query=self.query,
94 cfg=self.cfg,

/usr/local/lib/python3.10/dist-packages/gpt_researcher/actions/agent_creator.py in choose_agent(query, cfg, parent_query, cost_callback, headers)
42 print("⚠️ Error in reading JSON, attempting to repair JSON")
43 response_content = response.content.decode('utf-8') if hasattr(response, 'content') else str(response)
---> 44 return await handle_json_error(response_content)
45
46

/usr/local/lib/python3.10/dist-packages/gpt_researcher/actions/agent_creator.py in handle_json_error(response)
53 print(f"Error using json_repair: {e}")
54
---> 55 json_string = extract_json_with_regex(response)
56 if json_string:
57 try:

/usr/local/lib/python3.10/dist-packages/gpt_researcher/actions/agent_creator.py in extract_json_with_regex(response)
69
70 def extract_json_with_regex(response):
---> 71 json_match = re.search(r"{.*?}", response, re.DOTALL)
72 if json_match:
73 return json_match.group(0)

/usr/lib/python3.10/re.py in search(pattern, string, flags)
198 """Scan through string looking for a match to the pattern, returning
199 a Match object, or None if no match was found."""
--> 200 return _compile(pattern, flags).search(string)
201
202 def sub(pattern, repl, string, count=0, flags=0):

TypeError: expected string or bytes-like object

@alesaccoia
Copy link
Contributor

alesaccoia commented Jan 8, 2025

@ElishaKay I also bumped into it, taking care of this as soon as the other PR, 1070 is merged

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants