Skip to content

Commit

Permalink
docs(openai): add OpenAI chat examples using chat interface (#33)
Browse files Browse the repository at this point in the history
* feat: add OpenAI chat examples using chat interface

* style: format code using pre-commit hooks

* fix: fixes openai examples generated by all hands

---------

Co-authored-by: openhands <openhands@all-hands.dev>
  • Loading branch information
tito and openhands-agent authored Dec 30, 2024
1 parent 4598bdf commit 9fcc2e5
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/openai-async-chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import asyncio
from scope3ai import Scope3AI
from openai import AsyncOpenAI


async def main():
client = AsyncOpenAI()
scope3 = Scope3AI.init()

with scope3.trace() as tracer:
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content.strip())

impact = tracer.impact()
print(impact)
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")


if __name__ == "__main__":
asyncio.run(main())
30 changes: 30 additions & 0 deletions examples/openai-async-stream-chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import asyncio
from scope3ai import Scope3AI
from openai import AsyncOpenAI


async def main():
client = AsyncOpenAI()
scope3 = Scope3AI.init()

with scope3.trace() as tracer:
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True,
)
async for event in response:
if not event.choices:
continue
print(event.choices[0].delta.content, end="", flush=True)
print()

impact = tracer.impact()
print(impact)
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")


if __name__ == "__main__":
asyncio.run(main())
24 changes: 24 additions & 0 deletions examples/openai-chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from scope3ai import Scope3AI
from openai import OpenAI


def main():
client = OpenAI()
scope3 = Scope3AI.init()

with scope3.trace() as tracer:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content.strip())

impact = tracer.impact()
print(impact)
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")


if __name__ == "__main__":
main()
29 changes: 29 additions & 0 deletions examples/openai-stream-chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from scope3ai import Scope3AI
from openai import OpenAI


def main():
client = OpenAI()
scope3 = Scope3AI.init()

with scope3.trace() as tracer:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True,
)
for event in response:
if not event.choices:
continue
print(event.choices[0].delta.content, end="", flush=True)
print()

impact = tracer.impact()
print(impact)
print(f"Total Energy Wh: {impact.total_energy_wh}")
print(f"Total GCO2e: {impact.total_gco2e}")
print(f"Total MLH2O: {impact.total_mlh2o}")


if __name__ == "__main__":
main()

0 comments on commit 9fcc2e5

Please sign in to comment.