File size: 3,973 Bytes
9f3f2dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
"""Assistant.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1tbapNWoymz_Oq_M96lTAeUDZrvKcB2ji
"""

#Define Web Scraping function
def web_scrape(url, tag, id):
  headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
  }
  page = requests.get(url, headers=headers)
  soup = BeautifulSoup(page.content, 'html.parser')
  data = soup.find_all(tag, class_ = id)
  res = []
  i = 0
  while i < 6:
    res.append(data[i].get_text())
    i = i+1
  return res

#Define Google result scraping function
def google_scrape(query):
  url = "https://www.google.com/search?q=" + str(query)
  res = web_scrape(url, "div", "VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc lEBKkf")
  return res

#Define Bratanica web scraping function
def bratanica_scrape(query):
  headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
  }
  url = "https://www.britannica.com"
  page = requests.get(url + "/search?query=" + query, headers=headers)
  soup = BeautifulSoup(page.content, 'html.parser')
  link = soup.find_all("a", class_ ="font-weight-bold font-18")
  page = requests.get(url+link[0]["href"], headers=headers)
  soup = BeautifulSoup(page.content, 'html.parser')
  data = soup.find_all("p")
  res = []
  for i in data:
    res.append(i.get_text())
  return res

import requests
from bs4 import BeautifulSoup

import torch
from transformers import LlamaForCausalLM, LlamaTokenizer

# Hugging Face model_path
model_path = 'psmathur/orca_mini_3b'
tokenizer = LlamaTokenizer.from_pretrained(model_path)
model = LlamaForCausalLM.from_pretrained(
    model_path, torch_dtype=torch.float16, device_map='auto', load_in_4bit = True
)

#generate response function
def generate_text(system, instruction, gen_len, input=None):

    if input:
        prompt = f"### System:\n{system}\n\n### User:\n{instruction}\n\n### Context:\n{input}\n\n### Response:\n"
    else:
        prompt = f"### System:\n{system}\n\n### User:\n{instruction}\n\n### Response:\n"

    tokens = tokenizer.encode(prompt)
    tokens = torch.LongTensor(tokens).unsqueeze(0)
    tokens = tokens.to('cuda')

    instance = {'input_ids': tokens,'top_p': 1.5, 'temperature':0.3, 'generate_len': gen_len, 'top_k': 200}

    length = len(tokens[0])
    with torch.no_grad():
        rest = model.generate(
            input_ids=tokens,
            max_length=length+instance['generate_len'],
            use_cache=True,
            do_sample=True,
            top_p=instance['top_p'],
            temperature=instance['temperature'],
            top_k=instance['top_k']
        )
    output = rest[0][length:]
    string = tokenizer.decode(output, skip_special_tokens=True)
    return f'{string}'

def context(query):
  headers = {
      'Authorization': 'Bearer FGSCPX4IMD3MGEQHWOORJDR73RHT7LFZ',
  }

  params = {
      'v': '20230820',
      'q': query,
  }

  response = requests.get('https://api.wit.ai/message', params=params, headers=headers).json()

  return response["intents"][0]["name"]

convo = []
while True:
  q = input("Human:")
  convo.append(convo)

  if (context(q)) == "Does_not_require_context":
    system = "You are an AI chatbot that aims to be as helpful and funny as possible. You will be given the past conversation history to know what you were talking about before"
    instruction = q
    res = generate_text(system, instruction, 512, convo)
    convo.append("Bot:" + res)
    print(res)
  elif (context(q)) == "Requires_additional_context":
    system = "You are an AI chatbot that aims to be as helpful as possible. To do so, using the context, answer the question that the user asks you."
    instruction = q
    clue = google_scrape(q)
    res = generate_text(system, instruction, 512, clue[0])
    convo.append("Bot:" + res)
    print(res)