118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
import openai
|
|
import json
|
|
import subprocess
|
|
openai.api_key = "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"
|
|
|
|
import openai
|
|
|
|
def generate_answer_gpt(default_prompt, question, reference, completion_tokens):
|
|
"""
|
|
Generates an answer using GPT-4 or falls back to GPT-3.5-turbo if GPT-4 encounters an error.
|
|
Constructs a chat message sequence with a system prompt, reference, and user question.
|
|
|
|
Args:
|
|
default_prompt (str): The default system prompt for the model.
|
|
question (str): The user's question.
|
|
reference (str): Additional reference information.
|
|
completion_tokens (int): The maximum number of tokens for the model's response.
|
|
|
|
Returns:
|
|
dict: The response from the OpenAI API.
|
|
"""
|
|
try:
|
|
print("GPT REFERENCE: ", reference)
|
|
response = openai.ChatCompletion.create(
|
|
model="gpt-4",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": default_prompt
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": f"{reference}Question:{question}"
|
|
}
|
|
],
|
|
max_tokens=completion_tokens,
|
|
top_p=1,
|
|
frequency_penalty=0,
|
|
presence_penalty=0
|
|
)
|
|
except Exception as e:
|
|
print("Ein Fehler ist aufgetreten bei der Anfrage an GPT-4: ", e)
|
|
print("Sende nun die Anfrage an gpt-3.5-turbo-16k.")
|
|
try:
|
|
response = openai.ChatCompletion.create(
|
|
model="gpt-3.5-turbo-16k",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": default_prompt
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": f"{reference}Question:{question}"
|
|
}
|
|
],
|
|
max_tokens=completion_tokens,
|
|
top_p=1,
|
|
frequency_penalty=0,
|
|
presence_penalty=0
|
|
)
|
|
except Exception as e:
|
|
print("Ein Fehler ist aufgetreten bei der Anfrage an gpt-3.5-turbo-16k: ", e)
|
|
raise
|
|
|
|
return response
|
|
|
|
|
|
def rerank_documents_with_gpt35(system_prompt, question, reference, completion_tokens):
|
|
"""
|
|
Reranks documents using GPT-3.5-turbo based on the provided system prompt, question, and reference.
|
|
|
|
Args:
|
|
system_prompt (str): The system prompt for the model.
|
|
question (str): The user's question.
|
|
reference (str): Reference document text.
|
|
completion_tokens (int): The maximum number of tokens for the model's response.
|
|
|
|
Returns:
|
|
dict: The response from the OpenAI API.
|
|
"""
|
|
response = openai.ChatCompletion.create(
|
|
model="gpt-3.5-turbo-16k",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": system_prompt
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": f"{reference}Question:{question}"
|
|
|
|
}
|
|
],
|
|
max_tokens=completion_tokens,
|
|
top_p=1,
|
|
frequency_penalty=0,
|
|
presence_penalty=0
|
|
)
|
|
return response
|
|
def generate_embeddings_ada(input):
|
|
"""
|
|
NOTE: Unused cause we are gen. embeddings in the pipeline from the data service
|
|
Generates embeddings for the given input using the Ada model.
|
|
|
|
Args:
|
|
input (str): The input text to generate embeddings for.
|
|
|
|
Returns:
|
|
list: A list of embeddings.
|
|
"""
|
|
response = openai.Embedding.create(
|
|
input=input,
|
|
model="text-embedding-ada-002"
|
|
)
|
|
embeddings = response['data'][0]['embedding']
|
|
return embeddings
|