ka1kuk commited on
Commit
730b6b8
1 Parent(s): 6baa58f

Create H2o.py

Browse files
Files changed (1) hide show
  1. g4f/Provider/Providers/H2o.py +94 -0
g4f/Provider/Providers/H2o.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from requests import Session
2
+ from uuid import uuid4
3
+ from json import loads
4
+ import os
5
+ import json
6
+ import requests
7
+ from ...typing import sha256, Dict, get_type_hints
8
+
9
+ url = 'https://gpt-gm.h2o.ai'
10
+ model = ['falcon-40b', 'falcon-7b', 'llama-13b']
11
+ supports_stream = True
12
+ needs_auth = False
13
+ working = True
14
+
15
+ models = {
16
+ 'falcon-7b': 'h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3',
17
+ 'falcon-40b': 'h2oai/h2ogpt-gm-oasst1-en-2048-falcon-40b-v1',
18
+ 'llama-13b': 'h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-13b'
19
+ }
20
+
21
+ def _create_completion(model: str, messages: list, stream: bool, **kwargs):
22
+
23
+ conversation = ''
24
+ for message in messages:
25
+ conversation += '%s: %s\n' % (message['role'], message['content'])
26
+
27
+ conversation += 'assistant: '
28
+ session = requests.Session()
29
+
30
+ response = session.get("https://gpt-gm.h2o.ai/")
31
+ headers = {
32
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0",
33
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
34
+ "Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
35
+ "Content-Type": "application/x-www-form-urlencoded",
36
+ "Upgrade-Insecure-Requests": "1",
37
+ "Sec-Fetch-Dest": "document",
38
+ "Sec-Fetch-Mode": "navigate",
39
+ "Sec-Fetch-Site": "same-origin",
40
+ "Sec-Fetch-User": "?1",
41
+ "Referer": "https://gpt-gm.h2o.ai/r/jGfKSwU"
42
+ }
43
+ data = {
44
+ "ethicsModalAccepted": "true",
45
+ "shareConversationsWithModelAuthors": "true",
46
+ "ethicsModalAcceptedAt": "",
47
+ "activeModel": "h2oai/h2ogpt-gm-oasst1-en-2048-falcon-40b-v1",
48
+ "searchEnabled": "true"
49
+ }
50
+ response = session.post("https://gpt-gm.h2o.ai/settings", headers=headers, data=data)
51
+
52
+ headers = {
53
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0",
54
+ "Accept": "*/*",
55
+ "Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
56
+ "Content-Type": "application/json",
57
+ "Sec-Fetch-Dest": "empty",
58
+ "Sec-Fetch-Mode": "cors",
59
+ "Sec-Fetch-Site": "same-origin",
60
+ "Referer": "https://gpt-gm.h2o.ai/"
61
+ }
62
+ data = {
63
+ "model": models[model]
64
+ }
65
+
66
+ conversation_id = session.post("https://gpt-gm.h2o.ai/conversation", headers=headers, json=data)
67
+ data = {
68
+ "inputs": conversation,
69
+ "parameters": {
70
+ "temperature": kwargs.get('temperature', 0.4),
71
+ "truncate": kwargs.get('truncate', 2048),
72
+ "max_new_tokens": kwargs.get('max_new_tokens', 1024),
73
+ "do_sample": kwargs.get('do_sample', True),
74
+ "repetition_penalty": kwargs.get('repetition_penalty', 1.2),
75
+ "return_full_text": kwargs.get('return_full_text', False)
76
+ },
77
+ "stream": True,
78
+ "options": {
79
+ "id": kwargs.get('id', str(uuid4())),
80
+ "response_id": kwargs.get('response_id', str(uuid4())),
81
+ "is_retry": False,
82
+ "use_cache": False,
83
+ "web_search_id": ""
84
+ }
85
+ }
86
+
87
+ response = session.post(f"https://gpt-gm.h2o.ai/conversation/{conversation_id.json()['conversationId']}", headers=headers, json=data)
88
+ generated_text = response.text.replace("\n", "").split("data:")
89
+ generated_text = json.loads(generated_text[-1])
90
+
91
+ return generated_text["generated_text"]
92
+
93
+ params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
94
+ '(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])