CISCai commited on
Commit
aa9ae26
1 Parent(s): ca40449

Add grammar example

Browse files
Files changed (1) hide show
  1. README.md +49 -1
README.md CHANGED
@@ -178,7 +178,55 @@ from llama_cpp import Llama
178
 
179
  # Chat Completion API
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  llm = Llama(model_path="./Mistral-7B-Instruct-v0.3.IQ4_XS.gguf", n_gpu_layers=33, n_ctx=32768, temperature=0.0, repeat_penalty=1.1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  print(llm.create_chat_completion(
183
  messages = [
184
  {
@@ -201,7 +249,7 @@ print(llm.create_chat_completion(
201
  },
202
  { # The tool_call_id is from tool_calls and content is the result from the function call you made
203
  "role": "tool",
204
- "content": 20,
205
  "tool_call_id": "call__0_get_current_weather_cmpl-..."
206
  }
207
  ],
 
178
 
179
  # Chat Completion API
180
 
181
+ grammar = LlamaGrammar.from_json_schema(json.dumps({
182
+ "type": "array",
183
+ "items": {
184
+ "type": "object",
185
+ "required": [ "name", "arguments" ],
186
+ "properties": {
187
+ "name": {
188
+ "type": "string"
189
+ },
190
+ "arguments": {
191
+ "type": "object"
192
+ }
193
+ }
194
+ }
195
+ }))
196
+
197
  llm = Llama(model_path="./Mistral-7B-Instruct-v0.3.IQ4_XS.gguf", n_gpu_layers=33, n_ctx=32768, temperature=0.0, repeat_penalty=1.1)
198
+ response = llm.create_chat_completion(
199
+ messages = [
200
+ {
201
+ "role": "user",
202
+ "content": "What's the weather like in Oslo and Stockholm?"
203
+ }
204
+ ],
205
+ tools=[{
206
+ "type": "function",
207
+ "function": {
208
+ "name": "get_current_weather",
209
+ "description": "Get the current weather in a given location",
210
+ "parameters": {
211
+ "type": "object",
212
+ "properties": {
213
+ "location": {
214
+ "type": "string",
215
+ "description": "The city and state, e.g. San Francisco, CA"
216
+ },
217
+ "unit": {
218
+ "type": "string",
219
+ "enum": [ "celsius", "fahrenheit" ]
220
+ }
221
+ },
222
+ "required": [ "location" ]
223
+ }
224
+ }
225
+ }],
226
+ grammar = grammar
227
+ )
228
+ print(json.loads(response["choices"][0]["text"]))
229
+
230
  print(llm.create_chat_completion(
231
  messages = [
232
  {
 
249
  },
250
  { # The tool_call_id is from tool_calls and content is the result from the function call you made
251
  "role": "tool",
252
+ "content": "20",
253
  "tool_call_id": "call__0_get_current_weather_cmpl-..."
254
  }
255
  ],