Skip to content

Commit

Permalink
Added my langgraph_lib source code
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvdeep-7span committed Oct 24, 2024
0 parents commit b73fb40
Show file tree
Hide file tree
Showing 30 changed files with 1,557 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 ArjanCodes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions build/lib/langgraph_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# src/langgraph_fastapi/__init__.py

"""
LangGraph FastAPI Toolkit
A Python package providing tools and infrastructure to serve LangGraph agents using FastAPI.
"""

__version__ = "0.1.0"

from .client import AgentClient
from .schema import (
UserInput,
StreamInput,
AgentResponse,
ChatMessage,
Feedback,
FeedbackResponse,
ChatHistoryInput,
ChatHistory,
)
from .service import create_app
5 changes: 5 additions & 0 deletions build/lib/langgraph_lib/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# src/langgraph_fastapi/client/__init__.py

from .client import AgentClient

__all__ = ["AgentClient"]
240 changes: 240 additions & 0 deletions build/lib/langgraph_lib/client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# src/my_library/client/client.py

import json
import os
from typing import Any, AsyncGenerator, Generator, Optional, Dict

import httpx

from langgraph_lib.schema import (
ChatHistory,
ChatHistoryInput,
ChatMessage,
Feedback,
StreamInput,
UserInput,
)


class AgentClient:
"""Client for interacting with the agent service."""

def __init__(
self,
base_url: str = "http://localhost:80",
auth_secret: Optional[str] = None,
timeout: Optional[float] = None,
) -> None:
"""
Initialize the client.
Args:
base_url (str): The base URL of the agent service.
auth_secret (Optional[str]): Authentication secret for the agent service.
timeout (Optional[float]): Timeout for HTTP requests.
"""
self.base_url = base_url
self.auth_secret = auth_secret or os.getenv("AUTH_SECRET")
self.timeout = timeout

@property
def _headers(self) -> Dict[str, str]:
headers = {}
if self.auth_secret:
headers["Authorization"] = f"Bearer {self.auth_secret}"
return headers

async def ainvoke(
self, message: str, model: Optional[str] = None, thread_id: Optional[str] = None
) -> ChatMessage:
"""
Asynchronously invoke the agent. Only the final message is returned.
Args:
message (str): The message to send to the agent.
model (Optional[str]): LLM model to use for the agent.
thread_id (Optional[str]): Thread ID for continuing a conversation.
Returns:
ChatMessage: The response from the agent.
"""
request = UserInput(message=message, model=model, thread_id=thread_id)
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/invoke",
json=request.model_dump(),
headers=self._headers,
timeout=self.timeout,
)
if response.status_code == 200:
return ChatMessage.model_validate(response.json())
raise Exception(f"Error: {response.status_code} - {response.text}")

def invoke(
self, message: str, model: Optional[str] = None, thread_id: Optional[str] = None
) -> ChatMessage:
"""
Synchronously invoke the agent. Only the final message is returned.
Args:
message (str): The message to send to the agent.
model (Optional[str]): LLM model to use for the agent.
thread_id (Optional[str]): Thread ID for continuing a conversation.
Returns:
ChatMessage: The response from the agent.
"""
request = UserInput(message=message, model=model, thread_id=thread_id)
response = httpx.post(
f"{self.base_url}/invoke",
json=request.model_dump(),
headers=self._headers,
timeout=self.timeout,
)
if response.status_code == 200:
return ChatMessage.model_validate(response.json())
raise Exception(f"Error: {response.status_code} - {response.text}")

def _parse_stream_line(self, line: str) -> Optional[ChatMessage | str]:
line = line.strip()
if line.startswith("data: "):
data = line[6:]
if data == "[DONE]":
return None
try:
parsed = json.loads(data)
except Exception as e:
raise Exception(f"Error parsing message from server: {e}")
if parsed["type"] == "message":
try:
return ChatMessage.model_validate(parsed["content"])
except Exception as e:
raise Exception(f"Server returned invalid message: {e}")
elif parsed["type"] == "token":
return parsed["content"]
elif parsed["type"] == "error":
raise Exception(parsed["content"])
return None

def stream(
self,
message: str,
model: Optional[str] = None,
thread_id: Optional[str] = None,
stream_tokens: bool = True,
) -> Generator[ChatMessage | str, None, None]:
"""
Stream the agent's response synchronously.
Args:
message (str): The message to send to the agent.
model (Optional[str]): LLM model to use for the agent.
thread_id (Optional[str]): Thread ID for continuing a conversation.
stream_tokens (bool): Stream tokens as they are generated.
Yields:
Generator[ChatMessage | str, None, None]: The response from the agent.
"""
request = StreamInput(
message=message, model=model, thread_id=thread_id, stream_tokens=stream_tokens
)
with httpx.stream(
"POST",
f"{self.base_url}/stream",
json=request.model_dump(),
headers=self._headers,
timeout=self.timeout,
) as response:
if response.status_code != 200:
raise Exception(f"Error: {response.status_code} - {response.text}")
for line in response.iter_lines():
if line.strip():
parsed = self._parse_stream_line(line)
if parsed is None:
break
yield parsed

async def astream(
self,
message: str,
model: Optional[str] = None,
thread_id: Optional[str] = None,
stream_tokens: bool = True,
) -> AsyncGenerator[ChatMessage | str, None]:
"""
Stream the agent's response asynchronously.
Args:
message (str): The message to send to the agent.
model (Optional[str]): LLM model to use for the agent.
thread_id (Optional[str]): Thread ID for continuing a conversation.
stream_tokens (bool): Stream tokens as they are generated.
Yields:
AsyncGenerator[ChatMessage | str, None]: The response from the agent.
"""
request = StreamInput(
message=message, model=model, thread_id=thread_id, stream_tokens=stream_tokens
)
async with httpx.AsyncClient() as client:
async with client.stream(
"POST",
f"{self.base_url}/stream",
json=request.model_dump(),
headers=self._headers,
timeout=self.timeout,
) as response:
if response.status_code != 200:
raise Exception(f"Error: {response.status_code} - {response.text}")
async for line in response.aiter_lines():
if line.strip():
parsed = self._parse_stream_line(line)
if parsed is None:
break
yield parsed

async def acreate_feedback(
self, run_id: str, key: str, score: float, kwargs: Optional[Dict[str, Any]] = None
) -> None:
"""
Create a feedback record for a run.
Args:
run_id (str): The run ID to record feedback for.
key (str): The feedback key.
score (float): The feedback score.
kwargs (Optional[Dict[str, Any]]): Additional feedback parameters.
"""
request = Feedback(run_id=run_id, key=key, score=score, kwargs=kwargs or {})
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/feedback",
json=request.model_dump(),
headers=self._headers,
timeout=self.timeout,
)
if response.status_code != 200:
raise Exception(f"Error: {response.status_code} - {response.text}")

def get_history(self, thread_id: str) -> ChatHistory:
"""
Get chat history.
Args:
thread_id (str): Thread ID for identifying a conversation.
Returns:
ChatHistory: The chat history for the given thread ID.
"""
request = ChatHistoryInput(thread_id=thread_id)
response = httpx.post(
f"{self.base_url}/history",
json=request.model_dump(),
headers=self._headers,
timeout=self.timeout,
)
if response.status_code == 200:
response_object = response.json()
return ChatHistory.model_validate(response_object)
else:
raise Exception(f"Error: {response.status_code} - {response.text}")
25 changes: 25 additions & 0 deletions build/lib/langgraph_lib/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# src/langgraph_fastapi/schema/__init__.py

from .schema import (
UserInput,
StreamInput,
AgentResponse,
ChatMessage,
Feedback,
FeedbackResponse,
ChatHistoryInput,
ChatHistory,
convert_message_content_to_string,
)

__all__ = [
"UserInput",
"StreamInput",
"AgentResponse",
"ChatMessage",
"Feedback",
"FeedbackResponse",
"ChatHistoryInput",
"ChatHistory",
"convert_message_content_to_string",
]
Loading

0 comments on commit b73fb40

Please sign in to comment.