|
| 1 | +from langtrace_python_sdk import with_langtrace_root_span, langtrace |
| 2 | +from dotenv import load_dotenv |
| 3 | +from litellm import completion, acompletion |
| 4 | +import litellm |
| 5 | +import asyncio |
| 6 | + |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | + |
| 10 | +litellm.success_callback = ["langtrace"] |
| 11 | +langtrace.init() |
| 12 | +litellm.set_verbose = False |
| 13 | + |
| 14 | + |
| 15 | +@with_langtrace_root_span("Litellm Example OpenAI") |
| 16 | +def openAI(streaming=False): |
| 17 | + response = completion( |
| 18 | + model="gpt-3.5-turbo", |
| 19 | + messages=[ |
| 20 | + {"content": "respond only in Yoda speak.", "role": "system"}, |
| 21 | + {"content": "Hello, how are you?", "role": "user"}, |
| 22 | + ], |
| 23 | + stream=streaming, |
| 24 | + stream_options={"include_usage": True}, |
| 25 | + ) |
| 26 | + if streaming: |
| 27 | + for _ in response: |
| 28 | + pass |
| 29 | + else: |
| 30 | + return response |
| 31 | + |
| 32 | + |
| 33 | +# @with_langtrace_root_span("Litellm Example Anthropic Completion") |
| 34 | +def anthropic(streaming=False): |
| 35 | + try: |
| 36 | + |
| 37 | + response = completion( |
| 38 | + model="claude-2.1", |
| 39 | + messages=[ |
| 40 | + {"content": "respond only in Yoda speak.", "role": "system"}, |
| 41 | + {"content": "what is 2 + 2?", "role": "user"}, |
| 42 | + ], |
| 43 | + temperature=0.5, |
| 44 | + top_p=0.5, |
| 45 | + n=1, |
| 46 | + stream=streaming, |
| 47 | + stream_options={"include_usage": True}, |
| 48 | + ) |
| 49 | + # print(response) |
| 50 | + if streaming: |
| 51 | + for _ in response: |
| 52 | + pass |
| 53 | + else: |
| 54 | + return response |
| 55 | + except Exception as e: |
| 56 | + print("ERORRRR", e) |
| 57 | + |
| 58 | + |
| 59 | +# @with_langtrace_root_span("Litellm Example OpenAI Async Streaming") |
| 60 | +async def async_anthropic(streaming=False): |
| 61 | + response = await acompletion( |
| 62 | + model="claude-2.1", |
| 63 | + messages=[{"content": "Hello, how are you?", "role": "user"}], |
| 64 | + stream=streaming, |
| 65 | + stream_options={"include_usage": True}, |
| 66 | + temperature=0.5, |
| 67 | + top_p=0.5, |
| 68 | + n=1, |
| 69 | + ) |
| 70 | + if streaming: |
| 71 | + async for _ in response: |
| 72 | + pass |
| 73 | + else: |
| 74 | + return response |
| 75 | + |
| 76 | + |
| 77 | +def cohere(streaming=False): |
| 78 | + response = completion( |
| 79 | + model="command-r", |
| 80 | + messages=[ |
| 81 | + {"content": "respond only in Yoda speak.", "role": "system"}, |
| 82 | + {"content": "Hello, how are you?", "role": "user"}, |
| 83 | + ], |
| 84 | + stream=streaming, |
| 85 | + stream_options={"include_usage": True}, |
| 86 | + ) |
| 87 | + if streaming: |
| 88 | + for _ in response: |
| 89 | + pass |
| 90 | + else: |
| 91 | + return response |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + # openAI() |
| 96 | + anthropic(streaming=False) |
| 97 | + cohere(streaming=True) |
| 98 | + # asyncio.run(async_anthropic(streaming=True)) |
0 commit comments