YC-Chen commited on
Commit
57a1889
1 Parent(s): 568e672

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +38 -4
README.md CHANGED
@@ -115,15 +115,17 @@ prompt = prompt_engine.get_prompt(conversations)
115
  output_str = _inference(prompt, llm, params)
116
  result = prompt_engine.parse_generated_str(output_str)
117
 
118
- print(result) #
 
 
119
  ```
120
 
121
  **Function Calling**
122
 
123
  ```python
124
- from mtkresearch.llm.prompt import MRPromptV2
125
 
126
- sys_prompt = 'You are a helpful AI assistant built by MediaTek Research. The user you are helping speaks Traditional Chinese and comes from Taiwan.'
127
 
128
  functions = [
129
  {
@@ -146,6 +148,13 @@ functions = [
146
  }
147
  ]
148
 
 
 
 
 
 
 
 
149
  prompt_engine = MRPromptV2()
150
 
151
  # stage 1: query
@@ -158,10 +167,35 @@ prompt = prompt_engine.get_prompt(conversations, functions=functions)
158
  output_str = _inference(prompt, llm, params)
159
  result = prompt_engine.parse_generated_str(output_str)
160
 
161
- print(result) #
 
 
 
 
162
 
163
  # stage 2: execute called functions
 
 
 
 
 
 
 
164
 
165
  # stage 3: put executed results
 
 
 
 
 
 
 
 
 
 
166
 
 
 
 
 
167
  ```
 
115
  output_str = _inference(prompt, llm, params)
116
  result = prompt_engine.parse_generated_str(output_str)
117
 
118
+ print(result)
119
+ # {'role': 'assistant',
120
+ # 'content': '深度學習(Deep Learning)是一種機器學習方法,它模仿人類大腦的神經網路結構來處理複雜的數據和任務。在深度學習中,模型由多層人工神經元組成,每個神經元之間有權重連接,並通過非線性轉換進行計算。這些層與層之間的相互作用使模型能夠學習複雜的函數關係或模式,從而解決各種問題,如圖像識別、自然語言理解、語音辨識等。深度學習通常需要大量的數據和強大的計算能力,因此經常使用圖形處理器(GPU)或特殊的加速器來執行。'}
121
  ```
122
 
123
  **Function Calling**
124
 
125
  ```python
126
+ import json
127
 
128
+ from mtkresearch.llm.prompt import MRPromptV2
129
 
130
  functions = [
131
  {
 
148
  }
149
  ]
150
 
151
+ def faked_get_current_weather(location, unit=None):
152
+ return {'temperature': 30}
153
+
154
+ mapping = {
155
+ 'get_current_weather': faked_get_current_weather
156
+ }
157
+
158
  prompt_engine = MRPromptV2()
159
 
160
  # stage 1: query
 
167
  output_str = _inference(prompt, llm, params)
168
  result = prompt_engine.parse_generated_str(output_str)
169
 
170
+ print(result)
171
+ # {'role': 'assistant',
172
+ # 'tool_calls': [
173
+ # {'id': 'call_U9bYCBRAbF639uUqfwehwSbw', 'type': 'function',
174
+ # 'function': {'name': 'get_current_weather', 'arguments': '{"location": "台北, 台灣", "unit": "攝氏"}'}}]}
175
 
176
  # stage 2: execute called functions
177
+ conversations.append(result)
178
+
179
+ tool_call = result['tool_calls'][0]
180
+ func_name = tool_call['function']['name']
181
+ func = mapping[func_name]
182
+ arguments = json.loads(tool_call['function']['arguments'])
183
+ called_result = func(**arguments)
184
 
185
  # stage 3: put executed results
186
+ conversations.append(
187
+ {
188
+ 'role': 'tool',
189
+ 'tool_call_id': tool_call['id'],
190
+ 'name': func_name,
191
+ 'content': json.dumps(called_result)
192
+ }
193
+ )
194
+
195
+ prompt = prompt_engine.get_prompt(conversations, functions=functions)
196
 
197
+ output_str2 = _inference(prompt, llm, params)
198
+ result2 = prompt_engine.parse_generated_str(output_str2)
199
+ print(result2)
200
+ # {'role': 'assistant', 'content': '台北目前的溫度是攝氏30度。'}
201
  ```