Closed hcchengithub closed 7 months ago
I have the same issue, and found that LangChain does not support it yet. I resolved the issue by creating my custom LLM from BaseChatModel
. You can refer to the implementation of AzureChatOpenAI.
This way works https://python.langchain.com/docs/modules/model_io/chat/custom_chat_model/
@masonzhang Thank you very much!!
Did you look into exactly where it breaks?
For me when you try this way
llm = AzureChatOpenAI( default_headers={ "Authorization": f"Bearer {token}", "Ocp-Apim-Subscription-Key": "key", 'Cache-Control': 'no-cache', 'Content-Type': 'application/json', 'Ocp-Apim-Trace': 'true' }, azure_endpoint=APIM_BASE_URL, # Set your APIM base URL here model_name=COMPLETION_MODEL, # Model name for the request azure_deployment=COMPLETION_DEPLOYMENT, # Deployment name configured in Azure OpenAI max_tokens=SUMMARY_MAX_TOKENS, api_version="2024-05-01-preview", temperature=SUMMARY_TEMPERATURE )
This works for me.
When i try to invoke
llm.invoke("Hello")
It responds correctly with
AIMessage(content='Hello! How can I assist you today?', response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 8, 'total_tokens': 17}, 'model_name': 'gpt-35', 'system_fingerprint': None, 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}, id='xxx', usage_metadata={'input_tokens': 8, 'output_tokens': 9, 'total_tokens': 17})
When i try APIM with crewAI, it gives me below error
{'statusCode': 500, 'message': 'Internal server error', 'activityId': 'ba2f4130-8135-xxx'}
I tried to debug the trace and found that it fails at policy to identify [\"usage\"][\"total_tokens\"]
for some reason CrewAI is not passing a valid JSON and it doesn't have total token
as a tag.
I am still confused as it works for langchain even though in output its under usage_metadata
- Seems like CrewAI is changing something internally to save total token as Crew Output-- https://docs.crewai.com/core-concepts/Crews/#crew-output
format?
Any help would be much appreciated!
crewai 0.19.0 openai 1.13.3
Error message:
I got 'llm' through company internal authorization
and this llm itself works fine:
But failed with CrewAI:
Entire error messages: