-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconversational_rag.py
47 lines (31 loc) · 1.07 KB
/
conversational_rag.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
import os
import uuid
from ai21 import AI21Client
from ai21.models.chat import ChatMessage
from examples.studio import file_utils
messages = [
ChatMessage(content="<Write a question about your file here>", role="user"),
]
def upload_file(client: AI21Client):
file_name = f"test-file-{uuid.uuid4()}.txt"
file_path = os.getcwd()
path = os.path.join(file_path, file_name)
file_utils.create_file(file_path, file_name, "<your content goes here>")
return client.library.files.create(
file_path=path,
path=file_path,
)
def delete_file(client: AI21Client, file_id: str):
client.library.files.delete(file_id)
def main():
ai21_client = AI21Client()
file_id = upload_file(ai21_client)
chat_response = ai21_client.beta.conversational_rag.create(
messages=messages,
# you may add file IDs from your Studio library in order to question the model about their content
file_ids=[file_id],
)
print("Chat Response:", chat_response)
delete_file(ai21_client, file_id)
if __name__ == "__main__":
main()