-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(openai): add OpenAI chat examples using chat interface (#33)
* 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
1 parent
4598bdf
commit 9fcc2e5
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |