SimplerLLM 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- SimplerLLM/__init__.py +0 -0
- SimplerLLM/langauge/__init__.py +0 -0
- SimplerLLM/langauge/llm.py +136 -0
- SimplerLLM/langauge/llm_addons.py +56 -0
- SimplerLLM/langauge/llm_providers/__init__.py +0 -0
- SimplerLLM/langauge/llm_providers/gemeni_llm.py +125 -0
- SimplerLLM/langauge/llm_providers/openai_llm.py +341 -0
- SimplerLLM/prompts/__init__.py +0 -0
- SimplerLLM/prompts/prompt_builder.py +86 -0
- SimplerLLM/tools/__init__.py +0 -0
- SimplerLLM/tools/generic_text_loader.py +160 -0
- SimplerLLM/tools/json_helpers.py +130 -0
- SimplerLLM/tools/rapid_api.py +109 -0
- SimplerLLM/tools/serp.py +79 -0
- SimplerLLM-0.1.0.dist-info/METADATA +166 -0
- SimplerLLM-0.1.0.dist-info/RECORD +18 -0
- SimplerLLM-0.1.0.dist-info/WHEEL +5 -0
- SimplerLLM-0.1.0.dist-info/top_level.txt +1 -0
SimplerLLM/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import SimplerLLM.langauge.llm_providers.openai_llm as openai_llm
|
|
2
|
+
import SimplerLLM.langauge.llm_providers.gemeni_llm as gemeni_llm
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LLMProvider(Enum):
|
|
7
|
+
OPENAI = 1
|
|
8
|
+
GEMENI = 2
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class LLM:
|
|
12
|
+
def __init__(self, provider=LLMProvider.OPENAI, model_name="gpt-3.5-turbo", temperature=0.7,top_p=1.0):
|
|
13
|
+
self.provider = provider
|
|
14
|
+
self.model_name = model_name
|
|
15
|
+
self.temperature = temperature
|
|
16
|
+
self.top_p = top_p
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@staticmethod
|
|
21
|
+
def create(provider=LLMProvider.OPENAI, model_name="gpt-3.5-turbo", temperature=0.7,top_p=1.0):
|
|
22
|
+
if provider == LLMProvider.OPENAI:
|
|
23
|
+
return OpenAILLM(provider, model_name, temperature, top_p)
|
|
24
|
+
if provider == LLMProvider.GEMENI:
|
|
25
|
+
return GemeniLLM(provider, model_name, temperature,top_p)
|
|
26
|
+
else:
|
|
27
|
+
return LLM(provider, model_name, temperature)
|
|
28
|
+
|
|
29
|
+
def set_model(self, provider):
|
|
30
|
+
if not isinstance(provider, LLMProvider):
|
|
31
|
+
raise ValueError("Provider must be an instance of LLMProvider Enum")
|
|
32
|
+
self.provider = provider
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def generate_text(self, input_text):
|
|
36
|
+
if self.provider == LLMProvider.OPENAI:
|
|
37
|
+
return openai_llm.generate(input_text)
|
|
38
|
+
elif self.provider == LLMProvider.GEMENI:
|
|
39
|
+
return "generated with Gemeni"
|
|
40
|
+
else:
|
|
41
|
+
raise ValueError("Unsupported model")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class OpenAILLM(LLM):
|
|
49
|
+
def __init__(self, model, model_name, temperature,top_p):
|
|
50
|
+
super().__init__(model, model_name, temperature,top_p)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def generate_text(self, user_prompt, system_prompt="", model_name=None, temperature=None, top_p=None, max_tokens=500):
|
|
55
|
+
# Use instance values as defaults if not provided
|
|
56
|
+
model_name = model_name if model_name is not None else self.model_name
|
|
57
|
+
temperature = temperature if temperature is not None else self.temperature
|
|
58
|
+
top_p = top_p if top_p is not None else self.top_p
|
|
59
|
+
|
|
60
|
+
return openai_llm.generate_text(user_prompt=user_prompt, system_prompt=system_prompt,
|
|
61
|
+
model=model_name, temperature=temperature,
|
|
62
|
+
top_p=top_p, max_tokens=max_tokens)
|
|
63
|
+
|
|
64
|
+
async def generate_text_async(self, user_prompt, system_prompt="", model_name=None, temperature=None, top_p=None, max_tokens=500):
|
|
65
|
+
# Use instance values as defaults if not provided
|
|
66
|
+
model_name = model_name if model_name is not None else self.model_name
|
|
67
|
+
temperature = temperature if temperature is not None else self.temperature
|
|
68
|
+
top_p = top_p if top_p is not None else self.top_p
|
|
69
|
+
|
|
70
|
+
return await openai_llm.generate_text_async(user_prompt=user_prompt, system_prompt=system_prompt,
|
|
71
|
+
model=model_name, temperature=temperature,
|
|
72
|
+
top_p=top_p, max_tokens=max_tokens)
|
|
73
|
+
|
|
74
|
+
def generate_full_response(self, user_prompt, system_prompt="", model_name=None, temperature=None, top_p=None, max_tokens=500):
|
|
75
|
+
# Use instance values as defaults if not provided
|
|
76
|
+
model_name = model_name if model_name is not None else self.model_name
|
|
77
|
+
temperature = temperature if temperature is not None else self.temperature
|
|
78
|
+
top_p = top_p if top_p is not None else self.top_p
|
|
79
|
+
|
|
80
|
+
return openai_llm.generate_full_response(user_prompt=user_prompt, system_prompt=system_prompt,
|
|
81
|
+
model=model_name, temperature=temperature,
|
|
82
|
+
top_p=top_p, max_tokens=max_tokens)
|
|
83
|
+
|
|
84
|
+
async def generate_full_response_async(self, user_prompt, system_prompt="", model_name=None, temperature=None, top_p=None, max_tokens=500):
|
|
85
|
+
# Use instance values as defaults if not provided
|
|
86
|
+
model_name = model_name if model_name is not None else self.model_name
|
|
87
|
+
temperature = temperature if temperature is not None else self.temperature
|
|
88
|
+
top_p = top_p if top_p is not None else self.top_p
|
|
89
|
+
|
|
90
|
+
return await openai_llm.generate_full_response_async(user_prompt=user_prompt, system_prompt=system_prompt,
|
|
91
|
+
model=model_name, temperature=temperature,
|
|
92
|
+
top_p=top_p, max_tokens=max_tokens)
|
|
93
|
+
|
|
94
|
+
def generate_json_with_pydantic(self, user_prompt, pydantic_model,model_name):
|
|
95
|
+
return openai_llm.generate_json_with_pydantic(user_prompt=user_prompt,pydantic_model = pydantic_model,model_name=model_name)
|
|
96
|
+
|
|
97
|
+
async def generate_json_with_pydantic_async(self, user_prompt, pydantic_model,model_name):
|
|
98
|
+
return await openai_llm.generate_json_with_pydantic_async(user_prompt=user_prompt,pydantic_model = pydantic_model,model_name=model_name)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class GemeniLLM(LLM):
|
|
106
|
+
def __init__(self, model, model_name, temperature,top_p):
|
|
107
|
+
super().__init__(model, model_name, temperature,top_p)
|
|
108
|
+
|
|
109
|
+
def generate_text(self, user_prompt, model_name=None, temperature=None, top_p=None, max_tokens=500):
|
|
110
|
+
# Use instance values as defaults if not provided
|
|
111
|
+
model_name = model_name if model_name is not None else self.model_name
|
|
112
|
+
temperature = temperature if temperature is not None else self.temperature
|
|
113
|
+
top_p = top_p if top_p is not None else self.top_p
|
|
114
|
+
return gemeni_llm.generate_text(user_prompt=user_prompt,
|
|
115
|
+
model=model_name,temperature=temperature,
|
|
116
|
+
top_p=top_p, max_tokens=max_tokens)
|
|
117
|
+
|
|
118
|
+
def generate_full_response(self, user_prompt, model_name=None, temperature=None, top_p=None, max_tokens=500):
|
|
119
|
+
# Use instance values as defaults if not provided
|
|
120
|
+
model_name = model_name if model_name is not None else self.model_name
|
|
121
|
+
temperature = temperature if temperature is not None else self.temperature
|
|
122
|
+
top_p = top_p if top_p is not None else self.top_p
|
|
123
|
+
|
|
124
|
+
return gemeni_llm.generate_full_response(user_prompt=user_prompt,
|
|
125
|
+
model=model_name, temperature=temperature,
|
|
126
|
+
top_p=top_p, max_tokens=max_tokens)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from typing import Type
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from SimplerLLM.langauge.llm import LLM
|
|
5
|
+
|
|
6
|
+
from SimplerLLM.tools.json_helpers import (
|
|
7
|
+
extract_json_from_text,
|
|
8
|
+
convert_json_to_pydantic_model,
|
|
9
|
+
validate_json_with_pydantic_model,
|
|
10
|
+
generate_json_example_from_pydantic
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def generate_basic_pydantic_json_model(model_class: Type[BaseModel], prompt: str, llm_instance : LLM, max_retries: int = 3, initial_delay: float = 1.0) -> BaseModel:
|
|
15
|
+
"""
|
|
16
|
+
Generates a model instance based on a given prompt, retrying on validation errors.
|
|
17
|
+
|
|
18
|
+
:param model_class: The Pydantic model class to be used for validation and conversion.
|
|
19
|
+
:param prompt: The fully formatted prompt including the topic.
|
|
20
|
+
:param llm_instance: Instance of a large language model.
|
|
21
|
+
:param max_retries: Maximum number of retries on validation errors.
|
|
22
|
+
:param initial_delay: Initial delay in seconds before the first retry.
|
|
23
|
+
:return: Tuple containing either (model instance, None) or (None, error message).
|
|
24
|
+
"""
|
|
25
|
+
for attempt in range(max_retries + 1):
|
|
26
|
+
try:
|
|
27
|
+
json_model = generate_json_example_from_pydantic(model_class)
|
|
28
|
+
optimized_prompt = prompt + f'\n\n.The response should me a structured JSON format that matches the following JSON: {json_model}'
|
|
29
|
+
ai_response = llm_instance.generate_text(optimized_prompt)
|
|
30
|
+
|
|
31
|
+
if ai_response:
|
|
32
|
+
json_object = extract_json_from_text(ai_response)
|
|
33
|
+
|
|
34
|
+
validated, errors = validate_json_with_pydantic_model(model_class, json_object)
|
|
35
|
+
|
|
36
|
+
if not errors:
|
|
37
|
+
model_object = convert_json_to_pydantic_model(model_class, json_object[0])
|
|
38
|
+
return model_object
|
|
39
|
+
|
|
40
|
+
except Exception as e: # Replace with specific exception if possible
|
|
41
|
+
return f"Exception occurred: {e}"
|
|
42
|
+
|
|
43
|
+
if not ai_response and attempt < max_retries:
|
|
44
|
+
time.sleep(initial_delay * (2 ** attempt)) # Exponential backoff
|
|
45
|
+
continue
|
|
46
|
+
elif errors:
|
|
47
|
+
return f"Validation failed after {max_retries} retries: {errors}"
|
|
48
|
+
|
|
49
|
+
# Retry logic for validation errors
|
|
50
|
+
if errors and attempt < max_retries:
|
|
51
|
+
time.sleep(initial_delay * (2 ** attempt)) # Exponential backoff
|
|
52
|
+
continue
|
|
53
|
+
elif errors:
|
|
54
|
+
return f"Validation failed after {max_retries} retries: {errors}"
|
|
55
|
+
|
|
56
|
+
|
|
File without changes
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import google.generativeai as genai
|
|
2
|
+
import os
|
|
3
|
+
from dotenv import load_dotenv
|
|
4
|
+
import os
|
|
5
|
+
import asyncio
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
# Load environment variables
|
|
9
|
+
load_dotenv()
|
|
10
|
+
|
|
11
|
+
# Constants
|
|
12
|
+
GEMENI_API_KEY = os.getenv('GEMENI_API_KEY')
|
|
13
|
+
if GEMENI_API_KEY is None:
|
|
14
|
+
raise ValueError("Please set the OPENAI_API_KEY in .env file.")
|
|
15
|
+
|
|
16
|
+
MAX_RETRIES = os.getenv('MAX_RETRIES')
|
|
17
|
+
if MAX_RETRIES is not None:
|
|
18
|
+
MAX_RETRIES = int(MAX_RETRIES)
|
|
19
|
+
else:
|
|
20
|
+
MAX_RETRIES = 3 # Default value
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
RETRY_DELAY = os.getenv('RETRY_DELAY')
|
|
24
|
+
if RETRY_DELAY is not None:
|
|
25
|
+
RETRY_DELAY = int(RETRY_DELAY)
|
|
26
|
+
else:
|
|
27
|
+
RETRY_DELAY = 2 # Default value
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
STREAMING_DELAY = os.getenv('STREAMING_DELAY')
|
|
31
|
+
if STREAMING_DELAY is not None:
|
|
32
|
+
STREAMING_DELAY = float(RETRY_DELAY)
|
|
33
|
+
else:
|
|
34
|
+
STREAMING_DELAY = 0.1 # Default value
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
genai.configure(api_key=GEMENI_API_KEY)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def generate_text_basic(user_prompt, model):
|
|
41
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
42
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
43
|
+
|
|
44
|
+
if not model or not isinstance(model, str):
|
|
45
|
+
raise ValueError("model must be a non-empty string.")
|
|
46
|
+
|
|
47
|
+
model = genai.GenerativeModel(model)
|
|
48
|
+
|
|
49
|
+
for attempt in range(MAX_RETRIES):
|
|
50
|
+
try:
|
|
51
|
+
response = model.generate_content(
|
|
52
|
+
user_prompt
|
|
53
|
+
)
|
|
54
|
+
return response.text
|
|
55
|
+
|
|
56
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
57
|
+
if attempt < MAX_RETRIES - 1:
|
|
58
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
59
|
+
else:
|
|
60
|
+
# Log the error or inform the user
|
|
61
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
62
|
+
return None
|
|
63
|
+
|
|
64
|
+
def generate_text(user_prompt, model, max_tokens=2000, top_p=1.0, temperature=0.7):
|
|
65
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
66
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
67
|
+
|
|
68
|
+
if not model or not isinstance(model, str):
|
|
69
|
+
raise ValueError("model must be a non-empty string.")
|
|
70
|
+
|
|
71
|
+
model = genai.GenerativeModel(model)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
for attempt in range(MAX_RETRIES):
|
|
75
|
+
try:
|
|
76
|
+
response = model.generate_content(
|
|
77
|
+
user_prompt,
|
|
78
|
+
generation_config=genai.types.GenerationConfig(
|
|
79
|
+
candidate_count=1,
|
|
80
|
+
max_output_tokens=max_tokens,
|
|
81
|
+
temperature=temperature,top_p=top_p)
|
|
82
|
+
)
|
|
83
|
+
return response.text
|
|
84
|
+
|
|
85
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
86
|
+
if attempt < MAX_RETRIES - 1:
|
|
87
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
88
|
+
else:
|
|
89
|
+
# Log the error or inform the user
|
|
90
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
def generate_full_response(user_prompt, model, max_tokens=2000, top_p=1.0, temperature=0.7):
|
|
94
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
95
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
96
|
+
|
|
97
|
+
if not model or not isinstance(model, str):
|
|
98
|
+
raise ValueError("model must be a non-empty string.")
|
|
99
|
+
|
|
100
|
+
model = genai.GenerativeModel(model)
|
|
101
|
+
|
|
102
|
+
for attempt in range(MAX_RETRIES):
|
|
103
|
+
try:
|
|
104
|
+
response = model.generate_content(
|
|
105
|
+
user_prompt,
|
|
106
|
+
generation_config=genai.types.GenerationConfig(
|
|
107
|
+
candidate_count=1,
|
|
108
|
+
max_output_tokens=max_tokens,
|
|
109
|
+
temperature=temperature,top_p=top_p)
|
|
110
|
+
)
|
|
111
|
+
return response
|
|
112
|
+
|
|
113
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
114
|
+
if attempt < MAX_RETRIES - 1:
|
|
115
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
116
|
+
else:
|
|
117
|
+
# Log the error or inform the user
|
|
118
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
119
|
+
return None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
#add streaming
|
|
2
|
+
from openai import AsyncOpenAI
|
|
3
|
+
from openai import OpenAI
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
import os
|
|
6
|
+
import asyncio
|
|
7
|
+
import time
|
|
8
|
+
import instructor
|
|
9
|
+
|
|
10
|
+
# Load environment variables
|
|
11
|
+
load_dotenv()
|
|
12
|
+
|
|
13
|
+
# Constants
|
|
14
|
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
15
|
+
if OPENAI_API_KEY is None:
|
|
16
|
+
raise ValueError("Please set the OPENAI_API_KEY in .env file.")
|
|
17
|
+
|
|
18
|
+
MAX_RETRIES = os.getenv('MAX_RETRIES')
|
|
19
|
+
if MAX_RETRIES is not None:
|
|
20
|
+
MAX_RETRIES = int(MAX_RETRIES)
|
|
21
|
+
else:
|
|
22
|
+
MAX_RETRIES = 3 # Default value
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
RETRY_DELAY = os.getenv('RETRY_DELAY')
|
|
26
|
+
if RETRY_DELAY is not None:
|
|
27
|
+
RETRY_DELAY = int(RETRY_DELAY)
|
|
28
|
+
else:
|
|
29
|
+
RETRY_DELAY = 2 # Default value
|
|
30
|
+
|
|
31
|
+
STREAMING_DELAY = os.getenv('STREAMING_DELAY')
|
|
32
|
+
if STREAMING_DELAY is not None:
|
|
33
|
+
STREAMING_DELAY = float(RETRY_DELAY)
|
|
34
|
+
else:
|
|
35
|
+
STREAMING_DELAY = 0.1 # Default value
|
|
36
|
+
|
|
37
|
+
# Initialize the OpenAI clients
|
|
38
|
+
async_openai_client = AsyncOpenAI(api_key=OPENAI_API_KEY)
|
|
39
|
+
instructor.apatch(async_openai_client)
|
|
40
|
+
|
|
41
|
+
openai_client = OpenAI(api_key=OPENAI_API_KEY)
|
|
42
|
+
instructor.patch(openai_client)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def generate_text_basic(user_prompt, model):
|
|
46
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
47
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
48
|
+
for attempt in range(MAX_RETRIES):
|
|
49
|
+
try:
|
|
50
|
+
completion = openai_client.chat.completions.create(
|
|
51
|
+
model=model,
|
|
52
|
+
messages=[{"role": "user", "content": user_prompt}],
|
|
53
|
+
)
|
|
54
|
+
return [completion.choices[0].message.content][0]
|
|
55
|
+
|
|
56
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
57
|
+
if attempt < MAX_RETRIES - 1:
|
|
58
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
59
|
+
else:
|
|
60
|
+
# Log the error or inform the user
|
|
61
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
62
|
+
return None
|
|
63
|
+
|
|
64
|
+
async def generate_text_basic_async(user_prompt, model):
|
|
65
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
66
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
67
|
+
for attempt in range(MAX_RETRIES):
|
|
68
|
+
try:
|
|
69
|
+
completion = await async_openai_client.chat.completions.create(
|
|
70
|
+
model=model,
|
|
71
|
+
messages=[{"role": "user", "content": user_prompt}],
|
|
72
|
+
)
|
|
73
|
+
return [completion.choices[0].message.content][0]
|
|
74
|
+
|
|
75
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
76
|
+
if attempt < MAX_RETRIES - 1:
|
|
77
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
78
|
+
else:
|
|
79
|
+
# Log the error or inform the user
|
|
80
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
def generate_text(user_prompt,system_prompt, model, max_tokens=2000, top_p=1.0, temperature=0.7):
|
|
84
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
85
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
86
|
+
for attempt in range(MAX_RETRIES):
|
|
87
|
+
try:
|
|
88
|
+
completion = openai_client.chat.completions.create(
|
|
89
|
+
model=model,
|
|
90
|
+
messages = [
|
|
91
|
+
{"role": "system", "content": system_prompt},
|
|
92
|
+
{"role": "user", "content": user_prompt}
|
|
93
|
+
],
|
|
94
|
+
temperature=temperature,
|
|
95
|
+
max_tokens=max_tokens,
|
|
96
|
+
top_p=top_p,
|
|
97
|
+
)
|
|
98
|
+
return [completion.choices[0].message.content][0]
|
|
99
|
+
|
|
100
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
101
|
+
if attempt < MAX_RETRIES - 1:
|
|
102
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
103
|
+
else:
|
|
104
|
+
# Log the error or inform the user
|
|
105
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
106
|
+
return None
|
|
107
|
+
|
|
108
|
+
async def generate_text_async(user_prompt,system_prompt, model, max_tokens=2000, top_p=1.0, temperature=0.7 ):
|
|
109
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
110
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
111
|
+
for attempt in range(MAX_RETRIES):
|
|
112
|
+
try:
|
|
113
|
+
completion = await async_openai_client.chat.completions.create(
|
|
114
|
+
model=model,
|
|
115
|
+
messages = [
|
|
116
|
+
{"role": "system", "content": system_prompt},
|
|
117
|
+
{"role": "user", "content": user_prompt}
|
|
118
|
+
],
|
|
119
|
+
temperature=temperature,
|
|
120
|
+
max_tokens=max_tokens,
|
|
121
|
+
top_p=top_p,
|
|
122
|
+
)
|
|
123
|
+
return [completion.choices[0].message.content][0]
|
|
124
|
+
|
|
125
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
126
|
+
if attempt < MAX_RETRIES - 1:
|
|
127
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
128
|
+
else:
|
|
129
|
+
# Log the error or inform the user
|
|
130
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
def generate_full_response(user_prompt,system_prompt, model, max_tokens=2000, top_p=1.0, temperature=0.7):
|
|
134
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
135
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
136
|
+
for attempt in range(MAX_RETRIES):
|
|
137
|
+
try:
|
|
138
|
+
completion = openai_client.chat.completions.create(
|
|
139
|
+
model=model,
|
|
140
|
+
messages = [
|
|
141
|
+
{"role": "system", "content": system_prompt},
|
|
142
|
+
{"role": "user", "content": user_prompt}
|
|
143
|
+
],
|
|
144
|
+
temperature=temperature,
|
|
145
|
+
max_tokens=max_tokens,
|
|
146
|
+
top_p=top_p,
|
|
147
|
+
)
|
|
148
|
+
return completion
|
|
149
|
+
|
|
150
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
151
|
+
if attempt < MAX_RETRIES - 1:
|
|
152
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
153
|
+
else:
|
|
154
|
+
# Log the error or inform the user
|
|
155
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
156
|
+
return None
|
|
157
|
+
|
|
158
|
+
async def generate_full_response_async(user_prompt,system_prompt, model, max_tokens=2000, top_p=1.0, temperature=0.7):
|
|
159
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
160
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
161
|
+
for attempt in range(MAX_RETRIES):
|
|
162
|
+
try:
|
|
163
|
+
completion = await async_openai_client.chat.completions.create(
|
|
164
|
+
model=model,
|
|
165
|
+
messages = [
|
|
166
|
+
{"role": "system", "content": system_prompt},
|
|
167
|
+
{"role": "user", "content": user_prompt}
|
|
168
|
+
],
|
|
169
|
+
temperature=temperature,
|
|
170
|
+
max_tokens=max_tokens,
|
|
171
|
+
top_p=top_p,
|
|
172
|
+
)
|
|
173
|
+
return completion
|
|
174
|
+
|
|
175
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
176
|
+
if attempt < MAX_RETRIES - 1:
|
|
177
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
178
|
+
else:
|
|
179
|
+
# Log the error or inform the user
|
|
180
|
+
print(f"Failed to generate response after {MAX_RETRIES} attempts due to: {e}")
|
|
181
|
+
return None
|
|
182
|
+
|
|
183
|
+
def generate_json_with_pydantic(user_prompt, pydantic_model,model_name):
|
|
184
|
+
|
|
185
|
+
for attempt in range(MAX_RETRIES):
|
|
186
|
+
try:
|
|
187
|
+
resut: pydantic_model = openai_client.chat.completions.create(
|
|
188
|
+
model=model_name,
|
|
189
|
+
response_model=pydantic_model,
|
|
190
|
+
messages=[{"role": "user", "content": user_prompt}]
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
return resut
|
|
194
|
+
except Exception as e:
|
|
195
|
+
if attempt < MAX_RETRIES - 1: # don't wait after the last attempt
|
|
196
|
+
time.sleep(RETRY_DELAY * (2**attempt))
|
|
197
|
+
else:
|
|
198
|
+
print(f"Response generation exception after max retries: {e}")
|
|
199
|
+
return None
|
|
200
|
+
|
|
201
|
+
async def generate_json_with_pydantic_async(user_prompt, pydantic_model,model_name):
|
|
202
|
+
|
|
203
|
+
for attempt in range(MAX_RETRIES):
|
|
204
|
+
try:
|
|
205
|
+
resut: pydantic_model = await async_openai_client.chat.completions.create(
|
|
206
|
+
model=model_name,
|
|
207
|
+
response_model=pydantic_model,
|
|
208
|
+
messages=[{"role": "user", "content": user_prompt}]
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
return resut
|
|
212
|
+
except Exception as e:
|
|
213
|
+
if attempt < MAX_RETRIES - 1: # don't wait after the last attempt
|
|
214
|
+
await asyncio.sleep(RETRY_DELAY * (2**attempt))
|
|
215
|
+
else:
|
|
216
|
+
print(f"Response generation exception after max retries: {e}")
|
|
217
|
+
return None
|
|
218
|
+
|
|
219
|
+
def generate(user_prompt, model="gpt-3.5-turbo", max_retries=MAX_RETRIES, retry_delay=RETRY_DELAY, enable_streaming=False,print_response = False,streaming_delay = STREAMING_DELAY):
|
|
220
|
+
"""
|
|
221
|
+
Generates a text response for a given user prompt using the specified model.
|
|
222
|
+
Optionally enables streaming and prints the output.
|
|
223
|
+
|
|
224
|
+
Args:
|
|
225
|
+
user_prompt (str): The prompt to send to the model.
|
|
226
|
+
model (str): The model to use for the response. Default is "gpt-3.5-turbo".
|
|
227
|
+
max_retries (int): Maximum number of retries on failure. Default is MAX_RETRIES.
|
|
228
|
+
retry_delay (int): Delay between retries in seconds. Default is RETRY_DELAY.
|
|
229
|
+
enable_streaming (bool): Enable streaming of the output. Default is False.
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
str: The generated response, or None if it fails to generate.
|
|
233
|
+
"""
|
|
234
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
235
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
236
|
+
|
|
237
|
+
for attempt in range(max_retries):
|
|
238
|
+
try:
|
|
239
|
+
completion = openai_client.chat.completions.create(
|
|
240
|
+
model=model,
|
|
241
|
+
messages=[{"role": "user", "content": user_prompt}],
|
|
242
|
+
stream=enable_streaming
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
if enable_streaming:
|
|
246
|
+
responses = []
|
|
247
|
+
for message in completion:
|
|
248
|
+
responses.append(message.choices[0].delta.content)
|
|
249
|
+
# After collecting all the responses into the 'responses' list:
|
|
250
|
+
responses = [response for response in responses if response is not None and response != '']
|
|
251
|
+
if print_response:
|
|
252
|
+
print_responses(responses,streaming_delay)
|
|
253
|
+
return responses
|
|
254
|
+
else:
|
|
255
|
+
response = [completion.choices[0].message.content][0]
|
|
256
|
+
if print_response:
|
|
257
|
+
print_responses(response)
|
|
258
|
+
return response
|
|
259
|
+
|
|
260
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
261
|
+
if attempt < max_retries - 1:
|
|
262
|
+
time.sleep(retry_delay * (2**attempt))
|
|
263
|
+
else:
|
|
264
|
+
# Log the error or inform the user
|
|
265
|
+
print(f"Failed to generate response after {max_retries} attempts due to: {e}")
|
|
266
|
+
return None
|
|
267
|
+
|
|
268
|
+
async def async_generate(user_prompt, model="gpt-3.5-turbo", max_retries=MAX_RETRIES, retry_delay=RETRY_DELAY):
|
|
269
|
+
"""
|
|
270
|
+
Generates a text response for a given user prompt using the specified model.
|
|
271
|
+
|
|
272
|
+
Args:
|
|
273
|
+
user_prompt (str): The prompt to send to the model.
|
|
274
|
+
model (str): The model to use for the response. Default is "text-davinci-003".
|
|
275
|
+
max_retries (int): Maximum number of retries on failure. Default is MAX_RETRIES.
|
|
276
|
+
retry_delay (int): Delay between retries in seconds. Default is RETRY_DELAY.
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
str: The generated response, or None if it fails to generate.
|
|
280
|
+
"""
|
|
281
|
+
if not user_prompt or not isinstance(user_prompt, str):
|
|
282
|
+
raise ValueError("user_prompt must be a non-empty string.")
|
|
283
|
+
|
|
284
|
+
for attempt in range(max_retries):
|
|
285
|
+
try:
|
|
286
|
+
completion = await async_openai_client.chat.completions.create(
|
|
287
|
+
model=model,
|
|
288
|
+
messages=[{"role": "user", "content": user_prompt}],
|
|
289
|
+
)
|
|
290
|
+
return completion.choices[0].message.content
|
|
291
|
+
|
|
292
|
+
except Exception as e: # Consider catching more specific exceptions
|
|
293
|
+
if attempt < max_retries - 1:
|
|
294
|
+
await asyncio.sleep(retry_delay * (2**attempt))
|
|
295
|
+
else:
|
|
296
|
+
# Log the error or inform the user
|
|
297
|
+
print(f"Failed to generate response after {max_retries} attempts due to: {e}")
|
|
298
|
+
return None
|
|
299
|
+
|
|
300
|
+
#GPT-Vision posponded to V2
|
|
301
|
+
# def analyze_image_with_vision(image_url, prompt, model="gpt-4-vision-preview",max_tokens=300):
|
|
302
|
+
# response = openai_client.chat.completions.create(
|
|
303
|
+
# model="model",
|
|
304
|
+
# messages=[
|
|
305
|
+
# {
|
|
306
|
+
# "role": "user",
|
|
307
|
+
# "content": [
|
|
308
|
+
# {"type": "text", "text": prompt},
|
|
309
|
+
# {
|
|
310
|
+
# "type": "image_url",
|
|
311
|
+
# "image_url": {
|
|
312
|
+
# "url": image_url,
|
|
313
|
+
# },
|
|
314
|
+
# },
|
|
315
|
+
# ],
|
|
316
|
+
# }
|
|
317
|
+
# ],
|
|
318
|
+
# max_tokens=max_tokens,
|
|
319
|
+
# )
|
|
320
|
+
|
|
321
|
+
return response.choices[0].message.content
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def print_responses(responses,streaming_delay = STREAMING_DELAY):
|
|
329
|
+
"""
|
|
330
|
+
Prints each value. If the input is a list, it prints each item. If it's a single value, it just prints that value.
|
|
331
|
+
|
|
332
|
+
Args:
|
|
333
|
+
values (list or any): A list of values or a single value to print.
|
|
334
|
+
"""
|
|
335
|
+
if isinstance(responses, list):
|
|
336
|
+
for value in responses:
|
|
337
|
+
print(value, end='', flush=True) # Flush ensures the output is immediately printed
|
|
338
|
+
time.sleep(streaming_delay) # Wait for 0.1 seconds before printing the next item
|
|
339
|
+
print() # Print a newline character at the end
|
|
340
|
+
else:
|
|
341
|
+
print(responses)
|
|
File without changes
|