rodrigomasini commited on
Commit
2a9b1c8
·
verified ·
1 Parent(s): 439b8d9

Create api-examples/api-example-chat.py

Browse files
Files changed (1) hide show
  1. api-examples/api-example-chat.py +81 -0
api-examples/api-example-chat.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import requests
4
+
5
+ # For local streaming, the websockets are hosted without ssl - http://
6
+ HOST = 'localhost:5000'
7
+ URI = f'http://{HOST}/api/v1/chat'
8
+
9
+ # For reverse-proxied streaming, the remote will likely host with ssl - https://
10
+ # URI = 'https://your-uri-here.trycloudflare.com/api/v1/chat'
11
+
12
+
13
+ def run(user_input, history):
14
+ request = {
15
+ 'user_input': user_input,
16
+ 'max_new_tokens': 250,
17
+ 'history': history,
18
+ 'mode': 'instruct', # Valid options: 'chat', 'chat-instruct', 'instruct'
19
+ 'character': 'Example',
20
+ 'instruction_template': 'Vicuna-v1.1', # Will get autodetected if unset
21
+ # 'context_instruct': '', # Optional
22
+ 'your_name': 'You',
23
+
24
+ 'regenerate': False,
25
+ '_continue': False,
26
+ 'stop_at_newline': False,
27
+ 'chat_generation_attempts': 1,
28
+ 'chat-instruct_command': 'Continue the chat dialogue below. Write a single reply for the character "<|character|>".\n\n<|prompt|>',
29
+
30
+ # Generation params. If 'preset' is set to different than 'None', the values
31
+ # in presets/preset-name.yaml are used instead of the individual numbers.
32
+ 'preset': 'None',
33
+ 'do_sample': True,
34
+ 'temperature': 0.7,
35
+ 'top_p': 0.1,
36
+ 'typical_p': 1,
37
+ 'epsilon_cutoff': 0, # In units of 1e-4
38
+ 'eta_cutoff': 0, # In units of 1e-4
39
+ 'tfs': 1,
40
+ 'top_a': 0,
41
+ 'repetition_penalty': 1.18,
42
+ 'repetition_penalty_range': 0,
43
+ 'top_k': 40,
44
+ 'min_length': 0,
45
+ 'no_repeat_ngram_size': 0,
46
+ 'num_beams': 1,
47
+ 'penalty_alpha': 0,
48
+ 'length_penalty': 1,
49
+ 'early_stopping': False,
50
+ 'mirostat_mode': 0,
51
+ 'mirostat_tau': 5,
52
+ 'mirostat_eta': 0.1,
53
+
54
+ 'seed': -1,
55
+ 'add_bos_token': True,
56
+ 'truncation_length': 2048,
57
+ 'ban_eos_token': False,
58
+ 'skip_special_tokens': True,
59
+ 'stopping_strings': []
60
+ }
61
+
62
+ response = requests.post(URI, json=request)
63
+
64
+ if response.status_code == 200:
65
+ result = response.json()['results'][0]['history']
66
+ print(json.dumps(result, indent=4))
67
+ print()
68
+ print(result['visible'][-1][1])
69
+
70
+
71
+ if __name__ == '__main__':
72
+ user_input = "Please give me a step-by-step guide on how to plant a tree in my backyard."
73
+
74
+ # Basic example
75
+ history = {'internal': [], 'visible': []}
76
+
77
+ # "Continue" example. Make sure to set '_continue' to True above
78
+ # arr = [user_input, 'Surely, here is']
79
+ # history = {'internal': [arr], 'visible': [arr]}
80
+
81
+ run(user_input, history)