openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.10.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/finetuning/answers-with-ft.py

142lines · modecode

1import openai
2import argparse
3
4
5def create_context(
6 question, search_file_id, max_len=1800, search_model="ada", max_rerank=10
7):
8 """
9 Create a context for a question by finding the most similar context from the search file.
10 :param question: The question
11 :param search_file_id: The file id of the search file
12 :param max_len: The maximum length of the returned context (in tokens)
13 :param search_model: The search model to use
14 :param max_rerank: The maximum number of reranking
15 :return: The context
16 """
17 results = openai.Engine(search_model).search(
18 search_model=search_model,
19 query=question,
20 max_rerank=max_rerank,
21 file=search_file_id,
22 return_metadata=True,
23 )
24 returns = []
25 cur_len = 0
26 for result in results["data"]:
27 cur_len += int(result["metadata"]) + 4
28 if cur_len > max_len:
29 break
30 returns.append(result["text"])
31 return "\n\n###\n\n".join(returns)
32
33
34def answer_question(
35 search_file_id="<SEARCH_FILE_ID>",
36 fine_tuned_qa_model="<FT_QA_MODEL_ID>",
37 question="Which country won the European Football championship in 2021?",
38 max_len=1800,
39 search_model="ada",
40 max_rerank=10,
41 debug=False,
42 stop_sequence=["\n", "."],
43 max_tokens=100,
44):
45 """
46 Answer a question based on the most similar context from the search file, using your fine-tuned model.
47 :param question: The question
48 :param fine_tuned_qa_model: The fine tuned QA model
49 :param search_file_id: The file id of the search file
50 :param max_len: The maximum length of the returned context (in tokens)
51 :param search_model: The search model to use
52 :param max_rerank: The maximum number of reranking
53 :param debug: Whether to output debug information
54 :param stop_sequence: The stop sequence for Q&A model
55 :param max_tokens: The maximum number of tokens to return
56 :return: The answer
57 """
58 context = create_context(
59 question,
60 search_file_id,
61 max_len=max_len,
62 search_model=search_model,
63 max_rerank=max_rerank,
64 )
65 if debug:
66 print("Context:\n" + context)
67 print("\n\n")
68 try:
69 response = openai.Completion.create(
70 model=fine_tuned_qa_model,
71 prompt=f"Answer the question based on the context below\n\nText: {context}\n\n---\n\nQuestion: {question}\nAnswer:",
72 temperature=0,
73 max_tokens=max_tokens,
74 top_p=1,
75 frequency_penalty=0,
76 presence_penalty=0,
77 stop=stop_sequence,
78 )
79 return response["choices"][0]["text"]
80 except Exception as e:
81 print(e)
82 return ""
83
84
85if __name__ == "__main__":
86 parser = argparse.ArgumentParser(
87 description="Rudimentary functionality of the answers endpoint with a fine-tuned Q&A model.",
88 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
89 )
90 parser.add_argument(
91 "--search_file_id", help="Search file id", required=True, type=str
92 )
93 parser.add_argument(
94 "--fine_tuned_qa_model", help="Fine-tuned QA model id", required=True, type=str
95 )
96 parser.add_argument(
97 "--question", help="Question to answer", required=True, type=str
98 )
99 parser.add_argument(
100 "--max_len",
101 help="Maximum length of the returned context (in tokens)",
102 default=1800,
103 type=int,
104 )
105 parser.add_argument(
106 "--search_model", help="Search model to use", default="ada", type=str
107 )
108 parser.add_argument(
109 "--max_rerank",
110 help="Maximum number of reranking for the search",
111 default=10,
112 type=int,
113 )
114 parser.add_argument(
115 "--debug", help="Print debug information (context used)", action="store_true"
116 )
117 parser.add_argument(
118 "--stop_sequence",
119 help="Stop sequences for the Q&A model",
120 default=["\n", "."],
121 nargs="+",
122 type=str,
123 )
124 parser.add_argument(
125 "--max_tokens",
126 help="Maximum number of tokens to return",
127 default=100,
128 type=int,
129 )
130 args = parser.parse_args()
131 response = answer_question(
132 search_file_id=args.search_file_id,
133 fine_tuned_qa_model=args.fine_tuned_qa_model,
134 question=args.question,
135 max_len=args.max_len,
136 search_model=args.search_model,
137 max_rerank=args.max_rerank,
138 debug=args.debug,
139 stop_sequence=args.stop_sequence,
140 max_tokens=args.max_tokens,
141 )
142 print(f"Answer:{response}")