-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add python sdk in monorepo (#706)
- Loading branch information
Showing
45 changed files
with
5,780 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,9 @@ | ||
dist | ||
.env | ||
build | ||
.DS_Store | ||
.vscode/settings.json | ||
__pycache__/ | ||
lunary/test.py | ||
lunary/demo.py | ||
data |
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,26 @@ | ||
<div align="center"> | ||
|
||
<img src="https://lunary.ai/logo.png" style='border-radius: 12px;' width="50"/> | ||
<h1>Lunary Python SDK</h1> | ||
|
||
**📈 Python monitoring for AI apps and agent** | ||
|
||
[website](https://lunary.ai) - [docs](https://lunary.ai/docs/py/) - ![PyPI - Version](https://img.shields.io/pypi/v/lunary) | ||
|
||
--- | ||
|
||
</div> | ||
|
||
Use it with any LLM model and custom agents (not limited to OpenAI). | ||
|
||
To get started, get a project ID by registering [here](https://lunary.ai). | ||
|
||
## 🛠️ Installation | ||
|
||
```bash | ||
pip install lunary | ||
``` | ||
|
||
## 📖 Documentation | ||
|
||
Full docs are available [here](https://lunary.ai/docs/py). |
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,27 @@ | ||
import os | ||
import asyncio | ||
from anthropic import AsyncAnthropic | ||
import lunary | ||
|
||
client = AsyncAnthropic( | ||
api_key=os.environ.get("ANTHROPIC_API_KEY"), # This is the default and can be omitted | ||
) | ||
lunary.monitor(client) | ||
|
||
|
||
async def main() -> None: | ||
stream = await client.messages.create( | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Hello, Claude", | ||
} | ||
], | ||
model="claude-3-opus-20240229", | ||
) | ||
for event in stream: | ||
pass | ||
|
||
|
||
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,26 @@ | ||
import os | ||
import asyncio | ||
from anthropic import AsyncAnthropic | ||
import lunary | ||
|
||
client = AsyncAnthropic( | ||
api_key=os.environ.get("ANTHROPIC_API_KEY"), # This is the default and can be omitted | ||
) | ||
lunary.monitor(client) | ||
|
||
|
||
async def main() -> None: | ||
message = await client.messages.create( | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Hello, Claude", | ||
} | ||
], | ||
model="claude-3-opus-20240229", | ||
) | ||
print(message.content) | ||
|
||
|
||
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,22 @@ | ||
import os | ||
from anthropic import Anthropic | ||
import lunary | ||
|
||
client = Anthropic( | ||
api_key=os.environ.get("ANTHROPIC_API_KEY"), | ||
) | ||
lunary.monitor(client) | ||
|
||
|
||
message = client.messages.create( | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Hello, Claude", | ||
} | ||
], | ||
model="claude-3-opus-20240229", | ||
) | ||
|
||
print(message.ro |
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,26 @@ | ||
import os | ||
from anthropic import Anthropic | ||
import lunary | ||
|
||
client = Anthropic( | ||
api_key=os.environ.get("ANTHROPIC_API_KEY"), | ||
) | ||
lunary.monitor(client) | ||
|
||
|
||
stream = client.messages.create( | ||
max_tokens=1024, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Hello, Claude", | ||
} | ||
], | ||
model="claude-3-opus-20240229", | ||
stream=True | ||
) | ||
|
||
for event in stream: | ||
pass | ||
|
||
|
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,86 @@ | ||
from anthropic import Anthropic | ||
import lunary | ||
|
||
client = Anthropic() | ||
lunary.monitor(client) | ||
|
||
response = client.messages.create( | ||
model="claude-3-5-sonnet-20241022", | ||
max_tokens=1024, | ||
tools=[ | ||
{ | ||
"name": "get_weather", | ||
"description": "Get the current weather in a given location", | ||
"input_schema": { | ||
"type": "object", | ||
"properties": { | ||
"location": { | ||
"type": "string", | ||
"description": "The city and state, e.g. San Francisco, CA", | ||
} | ||
}, | ||
"required": ["location"], | ||
}, | ||
} | ||
], | ||
messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}], | ||
) | ||
|
||
response = client.messages.create( | ||
model="claude-3-5-sonnet-20241022", | ||
max_tokens=1024, | ||
tools=[ | ||
{ | ||
"name": "get_weather", | ||
"description": "Get the current weather in a given location", | ||
"input_schema": { | ||
"type": "object", | ||
"properties": { | ||
"location": { | ||
"type": "string", | ||
"description": "The city and state, e.g. San Francisco, CA" | ||
}, | ||
"unit": { | ||
"type": "string", | ||
"enum": ["celsius", "fahrenheit"], | ||
"description": "The unit of temperature, either 'celsius' or 'fahrenheit'" | ||
} | ||
}, | ||
"required": ["location"] | ||
} | ||
} | ||
], | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "What's the weather like in San Francisco?" | ||
}, | ||
{ | ||
"role": "assistant", | ||
"content": [ | ||
{ | ||
"type": "text", | ||
"text": "<thinking>I need to use get_weather, and the user wants SF, which is likely San Francisco, CA.</thinking>" | ||
}, | ||
{ | ||
"type": "tool_use", | ||
"id": "toolu_01A09q90qw90lq917835lq9", | ||
"name": "get_weather", | ||
"input": {"location": "San Francisco, CA", "unit": "celsius"} | ||
} | ||
] | ||
}, | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "tool_result", | ||
"tool_use_id": "toolu_01A09q90qw90lq917835lq9", # from the API response | ||
"content": "65 degrees" # from running your tool | ||
} | ||
] | ||
} | ||
] | ||
) | ||
|
||
print(response) |
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,36 @@ | ||
import os, asyncio | ||
from openai import AsyncAzureOpenAI | ||
import lunary | ||
|
||
DEPLOYMENT_ID = os.environ.get("AZURE_OPENAI_DEPLOYMENT_ID") | ||
RESOURCE_NAME = os.environ.get("AZURE_OPENAI_RESOURCE_NAME") | ||
API_KEY = os.environ.get("AZURE_OPENAI_API_KEY") | ||
|
||
|
||
client = AsyncAzureOpenAI( | ||
api_version="2023-07-01-preview", | ||
api_key=API_KEY, | ||
azure_endpoint=f"https://{DEPLOYMENT_ID}.openai.azure.com", | ||
) | ||
|
||
lunary.monitor(client) | ||
|
||
async def main() -> None: | ||
stream = await client.chat.completions.create( | ||
model=RESOURCE_NAME, | ||
stream=True, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Say this is an async stream test", | ||
} | ||
], | ||
) | ||
async for chunk in stream: | ||
if not chunk.choices: | ||
continue | ||
print(chunk.choices[0].delta.content, end="") | ||
|
||
|
||
|
||
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,32 @@ | ||
import os, asyncio | ||
from openai import AsyncAzureOpenAI | ||
import lunary | ||
|
||
DEPLOYMENT_ID = os.environ.get("AZURE_OPENAI_DEPLOYMENT_ID") | ||
RESOURCE_NAME = os.environ.get("AZURE_OPENAI_RESOURCE_NAME") | ||
API_KEY = os.environ.get("AZURE_OPENAI_API_KEY") | ||
|
||
|
||
client = AsyncAzureOpenAI( | ||
api_version="2023-07-01-preview", | ||
api_key=API_KEY, | ||
azure_endpoint=f"https://{DEPLOYMENT_ID}.openai.azure.com", | ||
) | ||
|
||
lunary.monitor(client) | ||
|
||
async def main() -> None: | ||
completion = await client.chat.completions.create( | ||
model=RESOURCE_NAME, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Say this is an Async test", | ||
} | ||
], | ||
) | ||
print(completion.to_json()) | ||
|
||
|
||
|
||
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,27 @@ | ||
import os | ||
from openai import AzureOpenAI | ||
import lunary | ||
|
||
API_VERSION = os.environ.get("OPENAI_API_VERSION") | ||
API_KEY = os.environ.get("AZURE_OPENAI_API_KEY") | ||
AZURE_ENDPOINT = os.environ.get("AZURE_OPENAI_ENDPOINT") | ||
RESOURCE_NAME = os.environ.get("AZURE_OPENAI_RESOURCE_NAME") | ||
|
||
|
||
client = AzureOpenAI( | ||
api_version=API_VERSION, | ||
azure_endpoint=AZURE_ENDPOINT, | ||
api_key=API_KEY | ||
) | ||
lunary.monitor(client) | ||
|
||
completion = client.chat.completions.create( | ||
model=RESOURCE_NAME, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "How do I output all files in a directory using Python?", | ||
}, | ||
], | ||
) | ||
print(completion.to_json()) |
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,31 @@ | ||
import os | ||
from openai import AzureOpenAI | ||
import lunary | ||
|
||
DEPLOYMENT_ID = os.environ.get("AZURE_OPENAI_DEPLOYMENT_ID") | ||
RESOURCE_NAME = os.environ.get("AZURE_OPENAI_RESOURCE_NAME") | ||
API_KEY = os.environ.get("AZURE_OPENAI_API_KEY") | ||
|
||
|
||
client = AzureOpenAI( | ||
api_version="2023-07-01-preview", | ||
api_key=API_KEY, | ||
azure_endpoint=f"https://{DEPLOYMENT_ID}.openai.azure.com", | ||
) | ||
|
||
lunary.monitor(client) | ||
|
||
stream = client.chat.completions.create( | ||
model=RESOURCE_NAME, | ||
stream=True, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Say sync stream", | ||
}, | ||
], | ||
) | ||
for chunk in stream: | ||
if not chunk.choices: | ||
continue | ||
print(chunk.choices[0].delta.content, end="") |
Oops, something went wrong.