Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve telemetry hierarchy organization #2106

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/python/LangChainAPI/app/routers/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
Response,
status
)

from opentelemetry.trace import SpanKind

from foundationallm.config import Configuration, UserIdentity
from foundationallm.models.operations import (
LongRunningOperation,
Expand Down Expand Up @@ -79,7 +82,7 @@ async def submit_completion_request(
CompletionOperation
Object containing the operation ID and status.
"""
with tracer.start_span('submit_completion_request') as span:
with tracer.start_span('langchainapi_submit_completion_request', kind=SpanKind.CONSUMER) as span:
try:
# Get the operation_id from the completion request.
operation_id = completion_request.operation_id
Expand Down Expand Up @@ -126,7 +129,7 @@ async def create_completion_response(
"""
Generates the completion response for the specified completion request.
"""
with tracer.start_span(f'create_completion_response') as span:
with tracer.start_span(f'langchainapi_create_completion_response', kind=SpanKind.CONSUMER) as span:
try:
span.set_attribute('operation_id', operation_id)
span.set_attribute('instance_id', instance_id)
Expand Down Expand Up @@ -209,7 +212,7 @@ async def get_operation_status(
instance_id: str,
operation_id: str
) -> LongRunningOperation:
with tracer.start_span(f'get_operation_status') as span:
with tracer.start_span(f'langchainapi_get_operation_status', kind=SpanKind.CONSUMER) as span:
# Create an operations manager to get the operation status.
operations_manager = OperationsManager(raw_request.app.extra['config'])

Expand Down Expand Up @@ -244,7 +247,7 @@ async def get_operation_result(
instance_id: str,
operation_id: str
) -> CompletionResponse:
with tracer.start_span(f'get_operation_result') as span:
with tracer.start_span(f'langchainapi_get_operation_result', kind=SpanKind.CONSUMER) as span:
# Create an operations manager to get the operation result.
operations_manager = OperationsManager(raw_request.app.extra['config'])

Expand Down Expand Up @@ -277,7 +280,7 @@ async def get_operation_logs(
instance_id: str,
operation_id: str
) -> List[LongRunningOperationLogEntry]:
with tracer.start_span(f'get_operation_log') as span:
with tracer.start_span(f'langchainapi_get_operation_log', kind=SpanKind.CONSUMER) as span:
# Create an operations manager to get the operation log.
operations_manager = OperationsManager(raw_request.app.extra['config'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from foundationallm.models.resource_providers.configuration import APIEndpointConfiguration
from foundationallm.models.resource_providers.prompts import MultipartPrompt
from foundationallm.plugins import PluginManager
from foundationallm.telemetry import Telemetry

class LangChainAgentBase():
"""
Expand All @@ -50,6 +51,8 @@ def __init__(self, instance_id: str, user_identity: UserIdentity, config: Config
self.has_retriever = False
self.operations_manager = operations_manager

self.tracer = Telemetry.get_tracer('langchain-agent-base')

@abstractmethod
async def invoke_async(self, request: CompletionRequestBase) -> CompletionResponse:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
from langchain_core.output_parsers import StrOutputParser
from langgraph.prebuilt import create_react_agent

from opentelemetry.trace import SpanKind

from foundationallm.langchain.agents import LangChainAgentBase
from foundationallm.langchain.exceptions import LangChainException
from foundationallm.langchain.retrievers import RetrieverFactory, ContentArtifactRetrievalBase
Expand Down Expand Up @@ -182,7 +185,7 @@ def _validate_request(self, request: KnowledgeManagementCompletionRequest):
raise LangChainException("The objects property on the completion request cannot be null.", 400)

if request.agent.workflow is not None:

ai_model_object_id = request.agent.workflow.get_resource_object_id_properties(
ResourceProviderNames.FOUNDATIONALLM_AIMODEL,
AIModelResourceTypeNames.AI_MODELS,
Expand All @@ -192,7 +195,7 @@ def _validate_request(self, request: KnowledgeManagementCompletionRequest):
if ai_model_object_id is None:
raise LangChainException("The agent's workflow AI models requires a main_model.", 400)
self.ai_model = self._get_ai_model_from_object_id(ai_model_object_id.object_id, request.objects)

prompt_object_id = request.agent.workflow.get_resource_object_id_properties(
ResourceProviderNames.FOUNDATIONALLM_PROMPT,
PromptResourceTypeNames.PROMPTS,
Expand Down Expand Up @@ -403,7 +406,7 @@ async def invoke_async(self, request: KnowledgeManagementCompletionRequest) -> C
ResourceObjectIdPropertyNames.OBJECT_ROLE,
ResourceObjectIdPropertyValues.MAIN_MODEL
)

image_generation_deployment_model = request.objects[model_object_id.object_id]["deployment_name"]
api_endpoint_object_id = request.objects[model_object_id.object_id]["endpoint_object_id"]
image_generation_client = self._get_image_gen_language_model(api_endpoint_object_id=api_endpoint_object_id, objects=request.objects)
Expand Down Expand Up @@ -466,11 +469,11 @@ async def invoke_async(self, request: KnowledgeManagementCompletionRequest) -> C
messages = self._build_conversation_history_message_list(request.message_history, agent.conversation_history_settings.max_history)
else:
messages = []

messages.append(HumanMessage(content=parsed_user_prompt))

response = await graph.ainvoke(
{'messages': messages},
{'messages': messages},
config={"configurable": {"original_user_prompt": parsed_user_prompt, **({"recursion_limit": agent.workflow.graph_recursion_limit} if agent.workflow.graph_recursion_limit is not None else {})}}
)
# TODO: process tool messages with analysis results AIMessage with content='' but has addition_kwargs={'tool_calls';[...]}
Expand Down Expand Up @@ -507,7 +510,7 @@ async def invoke_async(self, request: KnowledgeManagementCompletionRequest) -> C
# Start External Agent workflow implementation
if (agent.workflow is not None and isinstance(agent.workflow, ExternalAgentWorkflow)):
# prepare tools
tool_factory = ToolFactory(self.plugin_manager)
tool_factory = ToolFactory(self.plugin_manager)
tools = []

parsed_user_prompt = request.user_prompt
Expand All @@ -529,18 +532,19 @@ async def invoke_async(self, request: KnowledgeManagementCompletionRequest) -> C
tools,
self.user_identity,
self.config)

# Get message history
if agent.conversation_history_settings.enabled:
if agent.conversation_history_settings.enabled:
messages = self._build_conversation_history_message_list(request.message_history, agent.conversation_history_settings.max_history)
else:
messages = []

response = await workflow.invoke_async(
operation_id=request.operation_id,
user_prompt=parsed_user_prompt,
message_history=messages
)
with self.tracer.start_span(f'langchain_invoke_external_workflow', kind=SpanKind.CONSUMER) as span:
response = await workflow.invoke_async(
operation_id=request.operation_id,
user_prompt=parsed_user_prompt,
message_history=messages
)
return response
# End External Agent workflow implementation

Expand Down
Loading