Skip to content

Commit

Permalink
Fix bug of ppt and googlescholar (#167)
Browse files Browse the repository at this point in the history
* fix bug of ppt and googlescholar

* Format required parameters
  • Loading branch information
liujiangning30 authored Mar 4, 2024
1 parent 605a921 commit e16a6bf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ cython_debug/
#.idea/
.vscode/
docs/*/_build/
tmp_dir/
2 changes: 1 addition & 1 deletion examples/internlm2_agent_web_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def initialize_chatbot(self, model, plugin_action):
belong='assistant',
end='<|action_end|>\n',
), ),
)
max_turn=7)

def render_user(self, prompt: str):
with st.chat_message('user'):
Expand Down
6 changes: 4 additions & 2 deletions lagent/actions/ppt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'template': None,
'title': 'Title Slide',
'single': 'Title and Content',
'two': 'Tow content',
'two': 'Two Content',
}
}

Expand All @@ -31,7 +31,7 @@ def create_file(self, theme: str, abs_location: str) -> dict:
"""Create a pptx file with specific themes.
Args:
theme (:class:`str`): the theme used
theme (:class:`str`): the theme used. The value should be one of ['Default'].
abs_location (:class:`str`): the ppt file's absolute location
Returns:
Expand Down Expand Up @@ -115,13 +115,15 @@ def add_text_image_page(self, title: str, bullet_items: str,
:class:`dict`: operation status
* status: the result of the execution
"""
from PIL import Image
layout_name = self.theme_mapping[self.pointer.slide_master.name]['two']
layout = next(i for i in self.pointer.slide_master.slide_layouts
if i.name == layout_name)
slide = self.pointer.slides.add_slide(layout)
ph_title, ph_body1, ph_body2 = slide.placeholders
ph_title.text = title
ph = ph_body2
image = Image.open(image)
image_pil = image.to_pil()
left = ph.left
width = ph.width
Expand Down
6 changes: 6 additions & 0 deletions lagent/agents/internlm2_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def format(self,
tool_name = api_info['name'].split('.')[0]
plugin['description'] = API_PREFIX.format(
tool_name=tool_name, description=plugin['description'])
# only keep required parameters
required_parameters = [
param for param in plugin['parameters']
if param['name'] in plugin['required']
]
plugin['parameters'] = required_parameters
plugin_descriptions.append(plugin)
plugin_prompt = self.plugin_prompt.format(
prompt=json.dumps(
Expand Down
27 changes: 14 additions & 13 deletions lagent/llms/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def __init__(self,
self.gen_params.update(stop_words_id=stop_words_id)
if self.gen_params['stop_words'] is not None and \
self.gen_params['stop_words_id'] is not None:
logger.warning("Both stop_words and stop_words_id are specified,"
"only stop_words_id will be used.")
logger.warning('Both stop_words and stop_words_id are specified,'
'only stop_words_id will be used.')

self._load_tokenizer(
path=path,
Expand Down Expand Up @@ -80,7 +80,7 @@ def _load_tokenizer(self, path: str, tokenizer_path: Optional[str],
tokenizer_path if tokenizer_path else path,
trust_remote_code=True,
**tokenizer_kwargs)

if self.tokenizer.pad_token_id is None:
if self.tokenizer.eos_token is not None:
logger.warning(
Expand All @@ -101,7 +101,7 @@ def _load_tokenizer(self, path: str, tokenizer_path: Optional[str],
'pad_token_id is not set for this tokenizer. Try to '
'set pad_token_id via passing '
'`pad_token_id={PAD_TOKEN_ID}` in model_cfg.')

def _load_model(self, path: str, model_kwargs: dict):
import torch
from transformers import AutoModel
Expand Down Expand Up @@ -302,13 +302,16 @@ def _load_model(self, path: str, model_kwargs: dict):
path, trust_remote_code=True, **model_kwargs)
self.model.eval()


class HFTransformerChat(HFTransformerCasualLM):
def __init__(self,
template_parser=APITemplateParser,
**kwargs):

def __init__(self, template_parser=APITemplateParser, **kwargs):
super().__init__(template_parser=template_parser, **kwargs)

def chat(self, inputs: Union[List[dict], List[List[dict]]], do_sample: bool = True, **kwargs):
def chat(self,
inputs: Union[List[dict], List[List[dict]]],
do_sample: bool = True,
**kwargs):
"""Return the chat completions in stream mode.
Args:
Expand All @@ -327,12 +330,10 @@ def chat(self, inputs: Union[List[dict], List[List[dict]]], do_sample: bool = Tr
query = prompt[-1]['content']
history = prompt[:-1]
try:
response, history = self.model.chat(self.tokenizer,
query,
history=history)
response, history = self.model.chat(
self.tokenizer, query, history=history)
except Exception as e:
# handle over-length input error
logger.warning(str(e))
response = ""
response = ''
return response

0 comments on commit e16a6bf

Please sign in to comment.