-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeminiScript.py
More file actions
71 lines (51 loc) · 1.99 KB
/
Copy pathgeminiScript.py
File metadata and controls
71 lines (51 loc) · 1.99 KB
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
import os
from dotenv import load_dotenv
import google.generativeai as genai
load_dotenv()
def initialize_model():
try:
api_key = os.getenv('GEMINI_API_KEY')
# print(api_key)
if api_key is None:
raise ValueError("API key not found. Make sure it's set in the .env file.")
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-pro')
return model
except Exception as e:
print(f"An error occurred during model initialization: {e}")
return None
#initialize model
model = initialize_model()
base_prompt = """
Carefully understand the query and reply accordingly.
Instructions:
- The response should be simple text no randome symbols. Simple text but beautifully formatted.
- You have to send a prefix either CODE_COPY_PASTE or JUST_SAY, to let me know whether the response contains some code or actions to execute or its just a normal conversation message.
- If code is asked in reponse don't shortern the code or write somethign like 'write this like before', 'define function as before'. I want the full complete code in reponse.
- If the code is the response then avoid writing paragraphs. start your response with the CODE_COPY_PASTE. and the code should start by CODE_START and end with CODE_END.
example responses :
*for normal conversation -
JUST_SAY Good Morning, How may I help you?
*for code based queries -
CODE_COPY_PASTE
CODE_START
(complete code here)
CODE_END
-------
Query:
"""
def generate_text(input_text):
try:
if model is None:
raise ValueError("Model is not initialized")
contents=base_prompt+input_text
print(contents)
response = model.generate_content(contents=contents)
processed_text = response.text.strip()
return processed_text
except Exception as e:
print(f"An error occurred: {e}")
return None
# input_text = "Write a merge sort code."
# generated_text = generate_text(input_text)
# print(generated_text)