forked from rungalileo/sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
61 lines (48 loc) · 1.67 KB
/
Copy pathtest.py
File metadata and controls
61 lines (48 loc) · 1.67 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
import os
import time
import openai # Using the standard OpenAI library
from galileo import GalileoLogger # Import GalileoLogger for logging
from dotenv import load_dotenv
load_dotenv()
# Initialize the GalileoLogger
logger = GalileoLogger(project="chatbot", log_stream="test")
# Initialize the standard OpenAI client
client = openai.OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
organization=os.environ.get("OPENAI_ORGANIZATION"),
)
# Start a trace
# prompt = f"Explain the following topic succinctly: Newton's First Law"
prompt = """
1. Explain Newton's First Law in one sentence of no more than fifteen (15) words.
2. Do not add any additional sentences, examples, parentheses, bullet points, or further clarifications.
3. Your answer must be exactly one sentence and must not exceed 15 words.
"""
trace = logger.start_trace(input=prompt)
# Record the start time for the LLM call
start_time = time.time_ns()
# Make the OpenAI API call directly
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "system", "content": prompt}],
)
# Record the end time for the LLM call
end_time = time.time_ns()
# Get the response content
response_content = response.choices[0].message.content.strip()
# Log the LLM call as a span
logger.add_llm_span(
input=prompt,
output=response_content,
model="gpt-4o",
duration_ns=end_time - start_time,
num_input_tokens=response.usage.prompt_tokens,
num_output_tokens=response.usage.completion_tokens,
total_tokens=response.usage.total_tokens,
)
# Conclude the trace
logger.conclude(output=response_content)
# Flush the traces to Galileo
logger.flush()
# Print the response
print(response_content)