File size: 1,696 Bytes
3281c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import time

from openai import OpenAI

from project_settings import environment, project_path


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--openai_api_key",
        default=environment.get("openai_api_key", default=None, dtype=str),
        type=str
    )
    parser.add_argument(
        "--document_file",
        default=(project_path / "data/三国演义.txt").as_posix(),
        type=str
    )
    args = parser.parse_args()
    return args


def main():
    args = get_args()

    client = OpenAI(
        api_key=args.openai_api_key
    )

    document = client.files.create(
        file=open(args.document_file, "rb"),
        purpose='assistants'
    )

    assistant = client.beta.assistants.create(
        name="小说专家",
        instructions="根据小说内容回答问题。",
        tools=[{"type": "retrieval"}],
        model="gpt-4-1106-preview",
        file_ids=[document.id]

    )
    print(f"assistant.id: {assistant.id}")

    thread = client.beta.threads.create()
    print(f"thread.id: {thread.id}")

    message = client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content="刘备和张飞是什么关系。"
    )

    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant.id,
    )

    run = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id
    )

    time.sleep(10)

    messages = client.beta.threads.messages.list(
        thread_id=thread.id
    )
    print(messages)

    return


if __name__ == '__main__':
    main()