-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibary.py
77 lines (59 loc) · 2.18 KB
/
libary.py
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
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Pinecone
from langchain.document_loaders import UnstructuredURLLoader, WebBaseLoader
import pinecone
import openai
from utils.secret_keys import OPENAI_API_KEY, PINECONE_API_KEY, PINECONE_ENVIRONNEMNT
# Create documents from a list of urls
def createAndSplitTheDocumentFromUrl(urls):
# Create the document
loader = WebBaseLoader(urls)
data = loader.load()
# Split the document
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(data)
return docs
# Connect and save a document in pinecone db
def saveDataInPineconeDb(api_key, environment, index_name, docs):
# initialize pinecone
pinecone.init(
api_key=api_key, # find at app.pinecone.io
environment=environment, # next to api key in console
)
embeddings = OpenAIEmbeddings(openai_api_key = OPENAI_API_KEY)
docsearch = Pinecone.from_documents(docs, embeddings, index_name=index_name)
return docsearch
# Connect to pinecode db and use a specific index
def connectToDbAndUseSpecificIndex(api_key, environment, index_name):
# initialize pinecone
pinecone.init(
api_key=api_key, # find at app.pinecone.io
environment=environment, # next to api key in console
)
# Set the index
index = pinecone.Index(index_name)
# Create the openAI Embedings
embeddings = OpenAIEmbeddings(openai_api_key = OPENAI_API_KEY)
# Connect to the vector database
vectorstore = Pinecone(index, embeddings.embed_query, "text")
# Return the database
return vectorstore
# Talk to GPT
def askGPT(text):
openai.api_key = OPENAI_API_KEY
# Method for GPT4 (You can use it if you have access to the GPT4 API)
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "user", "content": text}
]
)
# Method for GPT3.5
# response = openai.Completion.create(
# model="gpt-3.5-turbo",
# messages=[
# {"role": "user", "content": text}
# ]
# )
return print(response.choices[0]["message"]["content"])