Update components/LLM.py
Browse files- components/LLM.py +36 -12
components/LLM.py
CHANGED
|
@@ -1,32 +1,34 @@
|
|
| 1 |
import openai
|
| 2 |
import time
|
|
|
|
|
|
|
| 3 |
|
| 4 |
#Call to Openai Chat Model
|
| 5 |
def chatopenai(finalprompt):
|
| 6 |
response = openai.ChatCompletion.create(
|
| 7 |
-
model=
|
| 8 |
messages=finalprompt,
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
API_response = response
|
| 12 |
assistant_response = response.choices[0].message['content']
|
| 13 |
usedtokens = response["usage"]["total_tokens"]
|
| 14 |
-
return
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
default_assistant_response = "Sorry, I'm having issue with OpenAI API, it happens... could you retry?"
|
| 19 |
default_used_tokens = 0
|
| 20 |
-
|
| 21 |
for _ in range(max_retries):
|
| 22 |
try:
|
| 23 |
response = openai.ChatCompletion.create(
|
| 24 |
-
model=
|
| 25 |
messages=finalprompt,
|
| 26 |
-
|
|
|
|
| 27 |
assistant_response = response.choices[0].message['content']
|
| 28 |
used_tokens = response["usage"]["total_tokens"]
|
| 29 |
-
return
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Encountered an error: {e}")
|
| 32 |
print("Retrying the API call...")
|
|
@@ -34,4 +36,26 @@ def chatopenaiandretry(finalprompt, max_retries=3, retry_delay=1):
|
|
| 34 |
|
| 35 |
# If all retries fail, return dummy values
|
| 36 |
print("Maximum retries reached. Unable to get a response from OpenAI.")
|
| 37 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import time
|
| 3 |
+
import tiktoken
|
| 4 |
+
from components.configs import LLMtemp, gptmodel
|
| 5 |
|
| 6 |
#Call to Openai Chat Model
|
| 7 |
def chatopenai(finalprompt):
|
| 8 |
response = openai.ChatCompletion.create(
|
| 9 |
+
model=gptmodel,
|
| 10 |
messages=finalprompt,
|
| 11 |
+
temperature = LLMtemp,
|
| 12 |
+
)
|
| 13 |
+
# API_response = response should I want to store the full API returned content...
|
| 14 |
assistant_response = response.choices[0].message['content']
|
| 15 |
usedtokens = response["usage"]["total_tokens"]
|
| 16 |
+
return assistant_response, usedtokens
|
| 17 |
|
| 18 |
+
#Call to Openai Chat Model 3 times, if it fails will return default messages back
|
| 19 |
+
def chatopenairetry(finalprompt, max_retries=3, retry_delay=1):
|
| 20 |
default_assistant_response = "Sorry, I'm having issue with OpenAI API, it happens... could you retry?"
|
| 21 |
default_used_tokens = 0
|
|
|
|
| 22 |
for _ in range(max_retries):
|
| 23 |
try:
|
| 24 |
response = openai.ChatCompletion.create(
|
| 25 |
+
model=gptmodel,
|
| 26 |
messages=finalprompt,
|
| 27 |
+
temperature = LLMtemp,
|
| 28 |
+
)
|
| 29 |
assistant_response = response.choices[0].message['content']
|
| 30 |
used_tokens = response["usage"]["total_tokens"]
|
| 31 |
+
return assistant_response, used_tokens
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Encountered an error: {e}")
|
| 34 |
print("Retrying the API call...")
|
|
|
|
| 36 |
|
| 37 |
# If all retries fail, return dummy values
|
| 38 |
print("Maximum retries reached. Unable to get a response from OpenAI.")
|
| 39 |
+
return default_assistant_response, default_used_tokens
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Call to Openai with the stream of content
|
| 43 |
+
def chatopenaistream(finalprompt, callback, LLMtemp=LLMtemp):
|
| 44 |
+
assistant_response = ""
|
| 45 |
+
for chunk in openai.ChatCompletion.create(
|
| 46 |
+
model=gptmodel,
|
| 47 |
+
messages=finalprompt,
|
| 48 |
+
temperature = LLMtemp,
|
| 49 |
+
stream=True,
|
| 50 |
+
):
|
| 51 |
+
content = chunk["choices"][0].get("delta", {}).get("content")
|
| 52 |
+
if content is not None:
|
| 53 |
+
assistant_response += content
|
| 54 |
+
yield callback(content) # Yield the result of the callback function
|
| 55 |
+
|
| 56 |
+
#token calculator to be run when using the streaming. Will calculate an estimate after the stream ended
|
| 57 |
+
def calculatetokens(finalprompt,assistant_response):
|
| 58 |
+
encoding = tiktoken.encoding_for_model(gptmodel)
|
| 59 |
+
usedtokens = len(encoding.encode(assistant_response + f"{finalprompt}"))
|
| 60 |
+
usedtokens=0
|
| 61 |
+
return usedtokens
|