-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext_generation.py
43 lines (33 loc) · 1.16 KB
/
text_generation.py
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
import os
from dotenv import load_dotenv
from groq import Groq
load_dotenv()
def log_error(message):
"""Logs error messages to a file."""
with open("error_logs.txt", "a") as log_file:
log_file.write(f"{message}\n")
# 1. Text Generation
def generate_text(prompt):
"""
Generates text based on a given prompt using the Groq Llama model.
"""
try:
# Get the API key from the environment variables
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
raise ValueError("API key is missing. Please check your .env file.")
# Set up the Groq client with the API key
client = Groq(api_key=api_key)
# Send a chat completion request
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": prompt}
],
model="llama3-8b-8192", # Groq model ID
)
# Return the content of the first choice
return chat_completion.choices[0].message.content
except Exception as e:
error_message = f"Error during summarization: {e}"
log_error(error_message)
return error_message