Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat (new llm): add support for openrouter (#3042)

Salem Korayem6b4c8e76e6b6
diff --git a/api/core/model_runtime/model_providers/_position.yaml b/api/core/model_runtime/model_providers/_position.yaml
index 7b4416f44ed..c06f1229849 100644
--- a/api/core/model_runtime/model_providers/_position.yaml
+++ b/api/core/model_runtime/model_providers/_position.yaml
@@ -6,6 +6,7 @@
- cohere
- bedrock
- togetherai
+- openrouter
- ollama
- mistralai
- groq
diff --git a/api/core/model_runtime/model_providers/openrouter/__init__.py b/api/core/model_runtime/model_providers/openrouter/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/api/core/model_runtime/model_providers/openrouter/_assets/openrouter.svg b/api/core/model_runtime/model_providers/openrouter/_assets/openrouter.svg
new file mode 100644
index 00000000000..2e9590d923b
--- /dev/null
+++ b/api/core/model_runtime/model_providers/openrouter/_assets/openrouter.svg
@@ -0,0 +1,11 @@
+<svg width="192" height="33" viewBox="0 0 192 33" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path d="M36.456 26.24C34.632 26.24 33.12 25.872 31.92 25.136C30.72 24.4 29.816 23.368 29.208 22.04C28.616 20.712 28.32 19.16 28.32 17.384C28.32 15.608 28.616 14.064 29.208 12.752C29.816 11.44 30.72 10.424 31.92 9.704C33.136 8.968 34.656 8.6 36.48 8.6C38.208 8.6 39.672 8.968 40.872 9.704C42.088 10.424 43.008 11.448 43.632 12.776C44.256 14.088 44.568 15.632 44.568 17.408C44.568 19.184 44.248 20.736 43.608 22.064C42.984 23.376 42.072 24.4 40.872 25.136C39.672 25.872 38.2 26.24 36.456 26.24ZM36.456 24.68C37.208 24.68 37.84 24.52 38.352 24.2C38.88 23.864 39.304 23.384 39.624 22.76C39.944 22.136 40.176 21.376 40.32 20.48C40.464 19.584 40.536 18.56 40.536 17.408C40.536 16.256 40.464 15.232 40.32 14.336C40.176 13.44 39.944 12.68 39.624 12.056C39.304 11.432 38.888 10.96 38.376 10.64C37.864 10.32 37.232 10.16 36.48 10.16C35.728 10.16 35.088 10.32 34.56 10.64C34.032 10.96 33.608 11.432 33.288 12.056C32.968 12.68 32.728 13.44 32.568 14.336C32.424 15.232 32.352 16.256 32.352 17.408C32.352 18.56 32.424 19.584 32.568 20.48C32.728 21.376 32.968 22.136 33.288 22.76C33.608 23.384 34.032 23.864 34.56 24.2C35.088 24.52 35.72 24.68 36.456 24.68ZM46.2026 31.76V30.488H46.4906C46.7306 30.488 46.9786 30.448 47.2346 30.368C47.5066 30.304 47.7306 30.16 47.9066 29.936C48.0986 29.712 48.1946 29.376 48.1946 28.928V16.088C48.1946 15.592 48.1226 15.232 47.9786 15.008C47.8346 14.768 47.6426 14.608 47.4026 14.528C47.1626 14.448 46.8906 14.408 46.5866 14.408H46.4186V13.136H51.1226L51.5546 15.008H51.6506C52.0026 14.384 52.4666 13.88 53.0426 13.496C53.6346 13.096 54.3946 12.896 55.3226 12.896C56.9226 12.896 58.1386 13.432 58.9706 14.504C59.8186 15.576 60.2426 17.28 60.2426 19.616C60.2426 21.936 59.8266 23.632 58.9946 24.704C58.1786 25.76 56.9866 26.288 55.4186 26.288C54.5066 26.288 53.7626 26.12 53.1866 25.784C52.6266 25.432 52.1706 24.96 51.8186 24.368H51.6506C51.6826 24.592 51.7146 24.896 51.7466 25.28C51.7786 25.648 51.8026 26.008 51.8186 26.36C51.8346 26.728 51.8426 27.032 51.8426 27.272V28.928C51.8426 29.376 51.9306 29.712 52.1066 29.936C52.2986 30.16 52.5306 30.304 52.8026 30.368C53.0746 30.448 53.3226 30.488 53.5466 30.488H54.0746V31.76H46.2026ZM54.2906 24.512C54.8346 24.512 55.2666 24.336 55.5866 23.984C55.9226 23.616 56.1626 23.072 56.3066 22.352C56.4666 21.632 56.5466 20.728 56.5466 19.64C56.5466 18.568 56.4666 17.672 56.3066 16.952C56.1626 16.216 55.9226 15.656 55.5866 15.272C55.2666 14.888 54.8426 14.696 54.3146 14.696C53.6746 14.696 53.1706 14.888 52.8026 15.272C52.4506 15.64 52.2026 16.192 52.0586 16.928C51.9146 17.664 51.8426 18.56 51.8426 19.616C51.8426 20.72 51.9146 21.64 52.0586 22.376C52.2026 23.096 52.4506 23.632 52.8026 23.984C53.1706 24.336 53.6666 24.512 54.2906 24.512ZM68.7034 26.24C66.6714 26.24 65.1274 25.664 64.0714 24.512C63.0154 23.344 62.4874 21.72 62.4874 19.64C62.4874 17.384 63.0074 15.688 64.0474 14.552C65.0874 13.4 66.5514 12.824 68.4394 12.824C70.1834 12.824 71.5514 13.312 72.5434 14.288C73.5514 15.264 74.0554 16.704 74.0554 18.608V19.832H66.1834C66.2154 20.904 66.3594 21.784 66.6154 22.472C66.8714 23.144 67.2314 23.64 67.6954 23.96C68.1754 24.28 68.7434 24.44 69.3994 24.44C69.9594 24.44 70.4554 24.36 70.8874 24.2C71.3354 24.024 71.7194 23.792 72.0394 23.504C72.3594 23.2 72.6074 22.864 72.7834 22.496C73.0074 22.608 73.1674 22.768 73.2634 22.976C73.3754 23.168 73.4314 23.4 73.4314 23.672C73.4314 24.12 73.2634 24.544 72.9274 24.944C72.5914 25.328 72.0714 25.64 71.3674 25.88C70.6794 26.12 69.7914 26.24 68.7034 26.24ZM70.3834 18.296C70.3834 17.48 70.3194 16.784 70.1914 16.208C70.0634 15.616 69.8634 15.168 69.5914 14.864C69.3194 14.544 68.9514 14.384 68.4874 14.384C68.0394 14.384 67.6474 14.536 67.3114 14.84C66.9914 15.128 66.7354 15.568 66.5434 16.16C66.3674 16.736 66.2634 17.448 66.2314 18.296H70.3834ZM75.5503 26V24.728H75.5983C75.9663 24.728 76.2863 24.688 76.5583 24.608C76.8463 24.528 77.0703 24.376 77.2303 24.152C77.3903 23.912 77.4703 23.552 77.4703 23.072V15.968C77.4703 15.52 77.3983 15.184 77.2543 14.96C77.1103 14.736 76.9103 14.592 76.6543 14.528C76.3983 14.448 76.0943 14.408 75.7423 14.408H75.6703V13.136H80.7103L81.0223 14.84H81.1423C81.4623 14.232 81.8063 13.792 82.1743 13.52C82.5583 13.232 82.9663 13.048 83.3983 12.968C83.8463 12.872 84.3183 12.824 84.8143 12.824C86.0463 12.824 87.0143 13.192 87.7183 13.928C88.4223 14.664 88.7743 15.84 88.7743 17.456V23.024C88.7743 23.52 88.8303 23.888 88.9423 24.128C89.0703 24.368 89.2543 24.528 89.4943 24.608C89.7343 24.688 90.0303 24.728 90.3823 24.728H90.4543V26H85.1263V18.104C85.1263 17.064 84.9823 16.264 84.6943 15.704C84.4063 15.144 83.9103 14.864 83.2063 14.864C82.6783 14.864 82.2623 15.04 81.9583 15.392C81.6543 15.728 81.4383 16.184 81.3103 16.76C81.1823 17.32 81.1183 17.928 81.1183 18.584V23.168C81.1183 23.616 81.1903 23.952 81.3343 24.176C81.4783 24.4 81.6783 24.552 81.9343 24.632C82.1903 24.696 82.4943 24.728 82.8463 24.728H82.9183V26H75.5503ZM91.7501 26V24.728H92.7581C92.9821 24.728 93.1821 24.696 93.3581 24.632C93.5501 24.568 93.7021 24.432 93.8141 24.224C93.9261 24.016 93.9821 23.704 93.9821 23.288V11.6C93.9821 11.168 93.9261 10.848 93.8141 10.64C93.7021 10.432 93.5501 10.296 93.3581 10.232C93.1821 10.168 92.9821 10.136 92.7581 10.136H91.7501V8.864H99.4781C100.998 8.864 102.238 9.032 103.198 9.368C104.174 9.704 104.894 10.208 105.358 10.88C105.822 11.536 106.054 12.344 106.054 13.304C106.054 14.152 105.894 14.872 105.574 15.464C105.254 16.056 104.846 16.544 104.35 16.928C103.854 17.312 103.334 17.616 102.79 17.84L105.646 22.76C105.854 23.112 106.054 23.416 106.246 23.672C106.438 23.912 106.622 24.112 106.798 24.272C106.99 24.432 107.19 24.552 107.398 24.632C107.606 24.696 107.822 24.728 108.046 24.728H108.142V26H107.35C106.63 26 105.998 25.976 105.454 25.928C104.926 25.864 104.462 25.76 104.062 25.616C103.678 25.456 103.334 25.232 103.03 24.944C102.726 24.656 102.454 24.288 102.214 23.84L99.3581 18.56H97.7741V23.288C97.7741 23.704 97.8301 24.016 97.9421 24.224C98.0541 24.432 98.2061 24.568 98.3981 24.632C98.5901 24.696 98.7901 24.728 98.9981 24.728H100.03V26H91.7501ZM99.1181 17.168C99.8541 17.168 100.43 17.04 100.846 16.784C101.278 16.512 101.582 16.12 101.758 15.608C101.934 15.08 102.022 14.424 102.022 13.64C102.022 12.824 101.926 12.176 101.734 11.696C101.542 11.2 101.23 10.84 100.798 10.616C100.366 10.392 99.7901 10.28 99.0701 10.28H97.7741V17.168H99.1181ZM115.367 26.24C113.447 26.24 111.935 25.688 110.831 24.584C109.727 23.464 109.175 21.776 109.175 19.52C109.175 17.264 109.703 15.584 110.759 14.48C111.815 13.376 113.375 12.824 115.439 12.824C117.359 12.824 118.871 13.376 119.975 14.48C121.079 15.584 121.631 17.264 121.631 19.52C121.631 21.776 121.095 23.464 120.023 24.584C118.967 25.688 117.415 26.24 115.367 26.24ZM115.415 24.728C116.039 24.728 116.527 24.536 116.879 24.152C117.247 23.752 117.511 23.168 117.671 22.4C117.831 21.616 117.911 20.656 117.911 19.52C117.911 18.368 117.831 17.416 117.671 16.664C117.511 15.896 117.247 15.32 116.879 14.936C116.511 14.552 116.023 14.36 115.415 14.36C114.791 14.36 114.295 14.552 113.927 14.936C113.559 15.32 113.295 15.896 113.135 16.664C112.975 17.416 112.895 18.368 112.895 19.52C112.895 20.656 112.975 21.616 113.135 22.4C113.295 23.168 113.559 23.752 113.927 24.152C114.311 24.536 114.807 24.728 115.415 24.728ZM128.958 26.24C127.71 26.24 126.75 25.856 126.078 25.088C125.406 24.304 125.07 23.104 125.07 21.488V16.112C125.07 15.648 125.014 15.296 124.902 15.056C124.79 14.8 124.614 14.632 124.374 14.552C124.134 14.456 123.822 14.408 123.438 14.408H123.39V13.136H128.718V20.816C128.718 21.504 128.766 22.104 128.862 22.616C128.974 23.112 129.166 23.504 129.438 23.792C129.71 24.064 130.078 24.2 130.542 24.2C131.054 24.2 131.47 24.056 131.79 23.768C132.11 23.464 132.342 23.04 132.486 22.496C132.646 21.936 132.726 21.288 132.726 20.552V15.944C132.726 15.48 132.646 15.144 132.486 14.936C132.326 14.712 132.118 14.568 131.862 14.504C131.606 14.44 131.318 14.408 130.998 14.408H130.926V13.136H136.374V23.216C136.374 23.68 136.438 24.024 136.566 24.248C136.71 24.456 136.902 24.592 137.142 24.656C137.398 24.704 137.678 24.728 137.982 24.728H138.174V26H133.398L132.87 24.296H132.75C132.51 24.712 132.246 25.048 131.958 25.304C131.686 25.56 131.39 25.76 131.07 25.904C130.75 26.032 130.414 26.12 130.062 26.168C129.71 26.216 129.342 26.24 128.958 26.24ZM144.605 26.24C143.533 26.24 142.677 25.96 142.037 25.4C141.397 24.84 141.077 23.856 141.077 22.448V14.768H139.277V13.544C139.789 13.544 140.245 13.44 140.645 13.232C141.061 13.024 141.389 12.792 141.629 12.536C141.869 12.28 142.085 11.96 142.277 11.576C142.469 11.176 142.621 10.704 142.733 10.16H144.725V13.136H147.797V14.768H144.725V22.208C144.725 22.928 144.845 23.456 145.085 23.792C145.341 24.128 145.749 24.296 146.309 24.296C146.629 24.296 146.925 24.28 147.197 24.248C147.485 24.2 147.757 24.144 148.013 24.08V25.64C147.757 25.752 147.333 25.88 146.741 26.024C146.165 26.168 145.453 26.24 144.605 26.24ZM155.844 26.24C153.812 26.24 152.268 25.664 151.212 24.512C150.156 23.344 149.628 21.72 149.628 19.64C149.628 17.384 150.148 15.688 151.188 14.552C152.228 13.4 153.692 12.824 155.58 12.824C157.324 12.824 158.692 13.312 159.684 14.288C160.692 15.264 161.196 16.704 161.196 18.608V19.832H153.324C153.356 20.904 153.5 21.784 153.756 22.472C154.012 23.144 154.372 23.64 154.836 23.96C155.316 24.28 155.884 24.44 156.54 24.44C157.1 24.44 157.596 24.36 158.028 24.2C158.476 24.024 158.86 23.792 159.18 23.504C159.5 23.2 159.748 22.864 159.924 22.496C160.148 22.608 160.308 22.768 160.404 22.976C160.516 23.168 160.572 23.4 160.572 23.672C160.572 24.12 160.404 24.544 160.068 24.944C159.732 25.328 159.212 25.64 158.508 25.88C157.82 26.12 156.932 26.24 155.844 26.24ZM157.524 18.296C157.524 17.48 157.46 16.784 157.332 16.208C157.204 15.616 157.004 15.168 156.732 14.864C156.46 14.544 156.092 14.384 155.628 14.384C155.18 14.384 154.788 14.536 154.452 14.84C154.132 15.128 153.876 15.568 153.684 16.16C153.508 16.736 153.404 17.448 153.372 18.296H157.524ZM162.691 26V24.728H162.763C163.131 24.728 163.451 24.688 163.723 24.608C163.995 24.528 164.211 24.368 164.371 24.128C164.531 23.872 164.611 23.496 164.611 23V16.04C164.611 15.56 164.539 15.208 164.395 14.984C164.251 14.744 164.051 14.592 163.795 14.528C163.539 14.448 163.235 14.408 162.883 14.408H162.811V13.136H167.659L168.139 15.008H168.259C168.467 14.528 168.707 14.128 168.979 13.808C169.251 13.488 169.595 13.248 170.011 13.088C170.443 12.928 170.987 12.848 171.643 12.848C172.587 12.848 173.283 13.016 173.731 13.352C174.179 13.688 174.403 14.168 174.403 14.792C174.403 15.528 174.131 16.104 173.587 16.52C173.059 16.92 172.315 17.12 171.355 17.12C171.355 16.672 171.315 16.304 171.235 16.016C171.171 15.728 171.051 15.512 170.875 15.368C170.715 15.208 170.483 15.128 170.179 15.128C169.763 15.128 169.427 15.272 169.171 15.56C168.915 15.848 168.723 16.208 168.595 16.64C168.467 17.056 168.379 17.48 168.331 17.912C168.283 18.344 168.259 18.696 168.259 18.968V23.12C168.259 23.584 168.331 23.936 168.475 24.176C168.619 24.4 168.819 24.552 169.075 24.632C169.331 24.696 169.619 24.728 169.939 24.728H170.707V26H162.691Z" fill="black"/>
+<path d="M1.05858 16.1738C1.76158 16.1738 4.47988 15.5672 5.88589 14.7704C7.2919 13.9737 7.2919 13.9737 10.1977 11.9115C13.8766 9.30069 16.4779 10.1749 20.7428 10.1749" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M11.4182 13.6314L11.3787 13.6595C8.50565 15.6985 8.42504 15.7557 6.92566 16.6053C5.98567 17.138 4.74704 17.5436 3.75151 17.8089C2.80313 18.0615 1.71203 18.2829 1.05858 18.2829V14.0648C1.05075 14.0648 1.05422 14.0645 1.06984 14.0628C1.11491 14.0579 1.26116 14.042 1.52896 13.9926C1.84599 13.9341 2.24205 13.8458 2.6657 13.733C3.55657 13.4956 4.3801 13.1996 4.84612 12.9355C4.88175 12.9153 4.91635 12.8957 4.95001 12.8767C6.15007 12.1969 6.15657 12.1932 8.97708 10.1916C12.5199 7.67735 15.5815 7.83587 18.5849 7.99138C19.3056 8.0287 20.0229 8.06584 20.7428 8.06584V12.2839C19.6102 12.2839 18.6583 12.2419 17.8263 12.2053C15.1245 12.0862 13.685 12.0228 11.4182 13.6314Z" fill="black"/>
+<path d="M24.8671 10.2009L17.6613 14.3612V6.04059L24.8671 10.2009Z" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M17.6378 6L24.9139 10.2009L17.6378 14.4018V6ZM17.6847 6.08118V14.3206L24.8202 10.2009L17.6847 6.08118Z" fill="black"/>
+<path d="M0.917975 16.1764C1.62098 16.1764 4.33927 16.7831 5.74529 17.5799C7.1513 18.3766 7.1513 18.3766 10.0571 20.4388C13.736 23.0496 16.3373 22.1754 20.6022 22.1754" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M0.929234 18.2875C0.913615 18.2858 0.910145 18.2854 0.917975 18.2854V14.0674C1.57142 14.0674 2.66253 14.2888 3.61091 14.5414C4.60644 14.8066 5.84507 15.2123 6.78506 15.745C8.28444 16.5946 8.36505 16.6518 11.2381 18.6908L11.2776 18.7188C13.5444 20.3275 14.9839 20.2641 17.6857 20.145C18.5177 20.1083 19.4696 20.0664 20.6022 20.0664V24.2844C19.8823 24.2844 19.165 24.3216 18.4443 24.3589C15.4409 24.5144 12.3793 24.6729 8.83648 22.1587C6.01597 20.157 6.00947 20.1533 4.80941 19.4736C4.77575 19.4545 4.74115 19.4349 4.70551 19.4148C4.2395 19.1507 3.41597 18.8546 2.5251 18.6173C2.10145 18.5045 1.70538 18.4162 1.38836 18.3577C1.12056 18.3083 0.974309 18.2924 0.929234 18.2875Z" fill="black"/>
+<path d="M24.7265 22.1494L17.5207 17.9892V26.3097L24.7265 22.1494Z" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M17.4972 17.9486L24.7733 22.1494L17.4972 26.3503V17.9486ZM17.5441 18.0297V26.2691L24.6796 22.1494L17.5441 18.0297Z" fill="black"/>
+</svg>
diff --git a/api/core/model_runtime/model_providers/openrouter/_assets/openrouter_square.svg b/api/core/model_runtime/model_providers/openrouter/_assets/openrouter_square.svg
new file mode 100644
index 00000000000..ed81fc041fc
--- /dev/null
+++ b/api/core/model_runtime/model_providers/openrouter/_assets/openrouter_square.svg
@@ -0,0 +1,10 @@
+<svg width="25" height="21" viewBox="0 0 25 21" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path d="M1.05858 10.1738C1.76158 10.1738 4.47988 9.56715 5.88589 8.77041C7.2919 7.97367 7.2919 7.97367 10.1977 5.91152C13.8766 3.30069 16.4779 4.17486 20.7428 4.17486" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M11.4182 7.63145L11.3787 7.65951C8.50565 9.69845 8.42504 9.75566 6.92566 10.6053C5.98567 11.138 4.74704 11.5436 3.75151 11.8089C2.80313 12.0615 1.71203 12.2829 1.05858 12.2829V8.06483C1.05075 8.06483 1.05422 8.06445 1.06984 8.06276C1.11491 8.05788 1.26116 8.04203 1.52896 7.9926C1.84599 7.9341 2.24205 7.84582 2.6657 7.73296C3.55657 7.49564 4.3801 7.1996 4.84612 6.93552C4.88175 6.91533 4.91635 6.89573 4.95001 6.87666C6.15007 6.19693 6.15657 6.19325 8.97708 4.1916C12.5199 1.67735 15.5815 1.83587 18.5849 1.99138C19.3056 2.0287 20.0229 2.06584 20.7428 2.06584V6.28388C19.6102 6.28388 18.6583 6.24193 17.8263 6.20527C15.1245 6.08621 13.685 6.02278 11.4182 7.63145Z" fill="black"/>
+<path d="M24.8671 4.20087L17.6613 8.36117V0.0405881L24.8671 4.20087Z" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M17.6378 0L24.9139 4.20087L17.6378 8.40176V0ZM17.6847 0.0811762V8.32058L24.8202 4.20087L17.6847 0.0811762Z" fill="black"/>
+<path d="M0.917975 10.1764C1.62098 10.1764 4.33927 10.7831 5.74529 11.5799C7.1513 12.3766 7.1513 12.3766 10.0571 14.4388C13.736 17.0496 16.3373 16.1754 20.6022 16.1754" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M0.929234 12.2875C0.913615 12.2858 0.910145 12.2854 0.917975 12.2854V8.06741C1.57142 8.06741 2.66253 8.28878 3.61091 8.54142C4.60644 8.80663 5.84507 9.21231 6.78506 9.74497C8.28444 10.5946 8.36505 10.6518 11.2381 12.6908L11.2776 12.7188C13.5444 14.3275 14.9839 14.2641 17.6857 14.145C18.5177 14.1083 19.4696 14.0664 20.6022 14.0664V18.2844C19.8823 18.2844 19.165 18.3216 18.4443 18.3589C15.4409 18.5144 12.3793 18.6729 8.83648 16.1587C6.01597 14.157 6.00947 14.1533 4.80941 13.4736C4.77575 13.4545 4.74115 13.4349 4.70551 13.4148C4.2395 13.1507 3.41597 12.8546 2.5251 12.6173C2.10145 12.5045 1.70538 12.4162 1.38836 12.3577C1.12056 12.3083 0.974309 12.2924 0.929234 12.2875Z" fill="black"/>
+<path d="M24.7265 16.1494L17.5207 11.9892V20.3097L24.7265 16.1494Z" fill="black"/>
+<path fill-rule="evenodd" clip-rule="evenodd" d="M17.4972 11.9486L24.7733 16.1494L17.4972 20.3503V11.9486ZM17.5441 12.0297V20.2691L24.6796 16.1494L17.5441 12.0297Z" fill="black"/>
+</svg>
diff --git a/api/core/model_runtime/model_providers/openrouter/llm/__init__.py b/api/core/model_runtime/model_providers/openrouter/llm/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/api/core/model_runtime/model_providers/openrouter/llm/llm.py b/api/core/model_runtime/model_providers/openrouter/llm/llm.py
new file mode 100644
index 00000000000..bb62fc7bb2e
--- /dev/null
+++ b/api/core/model_runtime/model_providers/openrouter/llm/llm.py
@@ -0,0 +1,46 @@
+from collections.abc import Generator
+from typing import Optional, Union
+
+from core.model_runtime.entities.llm_entities import LLMResult
+from core.model_runtime.entities.message_entities import PromptMessage, PromptMessageTool
+from core.model_runtime.entities.model_entities import AIModelEntity
+from core.model_runtime.model_providers.openai_api_compatible.llm.llm import OAIAPICompatLargeLanguageModel
+
+
+class OpenRouterLargeLanguageModel(OAIAPICompatLargeLanguageModel):
+
+ def _update_endpoint_url(self, credentials: dict):
+ credentials['endpoint_url'] = "https://openrouter.ai/api/v1"
+ return credentials
+
+ def _invoke(self, model: str, credentials: dict,
+ prompt_messages: list[PromptMessage], model_parameters: dict,
+ tools: Optional[list[PromptMessageTool]] = None, stop: Optional[list[str]] = None,
+ stream: bool = True, user: Optional[str] = None) \
+ -> Union[LLMResult, Generator]:
+ cred_with_endpoint = self._update_endpoint_url(credentials=credentials)
+
+ return super()._invoke(model, cred_with_endpoint, prompt_messages, model_parameters, tools, stop, stream, user)
+
+ def validate_credentials(self, model: str, credentials: dict) -> None:
+ cred_with_endpoint = self._update_endpoint_url(credentials=credentials)
+
+ return super().validate_credentials(model, cred_with_endpoint)
+
+ def _generate(self, model: str, credentials: dict, prompt_messages: list[PromptMessage], model_parameters: dict,
+ tools: Optional[list[PromptMessageTool]] = None, stop: Optional[list[str]] = None,
+ stream: bool = True, user: Optional[str] = None) -> Union[LLMResult, Generator]:
+ cred_with_endpoint = self._update_endpoint_url(credentials=credentials)
+
+ return super()._generate(model, cred_with_endpoint, prompt_messages, model_parameters, tools, stop, stream, user)
+
+ def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity:
+ cred_with_endpoint = self._update_endpoint_url(credentials=credentials)
+
+ return super().get_customizable_model_schema(model, cred_with_endpoint)
+
+ def get_num_tokens(self, model: str, credentials: dict, prompt_messages: list[PromptMessage],
+ tools: Optional[list[PromptMessageTool]] = None) -> int:
+ cred_with_endpoint = self._update_endpoint_url(credentials=credentials)
+
+ return super().get_num_tokens(model, cred_with_endpoint, prompt_messages, tools)
diff --git a/api/core/model_runtime/model_providers/openrouter/openrouter.py b/api/core/model_runtime/model_providers/openrouter/openrouter.py
new file mode 100644
index 00000000000..81313fd29ac
--- /dev/null
+++ b/api/core/model_runtime/model_providers/openrouter/openrouter.py
@@ -0,0 +1,11 @@
+import logging
+
+from core.model_runtime.model_providers.__base.model_provider import ModelProvider
+
+logger = logging.getLogger(__name__)
+
+
+class OpenRouterProvider(ModelProvider):
+
+ def validate_provider_credentials(self, credentials: dict) -> None:
+ pass
diff --git a/api/core/model_runtime/model_providers/openrouter/openrouter.yaml b/api/core/model_runtime/model_providers/openrouter/openrouter.yaml
new file mode 100644
index 00000000000..48a70700bbf
--- /dev/null
+++ b/api/core/model_runtime/model_providers/openrouter/openrouter.yaml
@@ -0,0 +1,75 @@
+provider: openrouter
+label:
+ en_US: openrouter.ai
+icon_small:
+ en_US: openrouter_square.svg
+icon_large:
+ en_US: openrouter.svg
+background: "#F1EFED"
+help:
+ title:
+ en_US: Get your API key from openrouter.ai
+ zh_Hans: 从 openrouter.ai 获取 API Key
+ url:
+ en_US: https://openrouter.ai/keys
+supported_model_types:
+ - llm
+configurate_methods:
+ - customizable-model
+model_credential_schema:
+ model:
+ label:
+ en_US: Model Name
+ zh_Hans: 模型名称
+ placeholder:
+ en_US: Enter full model name
+ zh_Hans: 输入模型全称
+ credential_form_schemas:
+ - variable: api_key
+ required: true
+ label:
+ en_US: API Key
+ type: secret-input
+ placeholder:
+ zh_Hans: 在此输入您的 API Key
+ en_US: Enter your API Key
+ - variable: mode
+ show_on:
+ - variable: __model_type
+ value: llm
+ label:
+ en_US: Completion mode
+ type: select
+ required: false
+ default: chat
+ placeholder:
+ zh_Hans: 选择对话类型
+ en_US: Select completion mode
+ options:
+ - value: completion
+ label:
+ en_US: Completion
+ zh_Hans: 补全
+ - value: chat
+ label:
+ en_US: Chat
+ zh_Hans: 对话
+ - variable: context_size
+ label:
+ zh_Hans: 模型上下文长度
+ en_US: Model context size
+ required: true
+ type: text-input
+ default: "4096"
+ placeholder:
+ zh_Hans: 在此输入您的模型上下文长度
+ en_US: Enter your Model context size
+ - variable: max_tokens_to_sample
+ label:
+ zh_Hans: 最大 token 上限
+ en_US: Upper bound for max tokens
+ show_on:
+ - variable: __model_type
+ value: llm
+ default: "4096"
+ type: text-input
diff --git a/api/tests/integration_tests/model_runtime/openrouter/__init__.py b/api/tests/integration_tests/model_runtime/openrouter/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/api/tests/integration_tests/model_runtime/openrouter/test_llm.py b/api/tests/integration_tests/model_runtime/openrouter/test_llm.py
new file mode 100644
index 00000000000..c0164e64183
--- /dev/null
+++ b/api/tests/integration_tests/model_runtime/openrouter/test_llm.py
@@ -0,0 +1,118 @@
+import os
+from typing import Generator
+
+import pytest
+from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta
+from core.model_runtime.entities.message_entities import (AssistantPromptMessage, PromptMessageTool,
+ SystemPromptMessage, UserPromptMessage)
+from core.model_runtime.errors.validate import CredentialsValidateFailedError
+from core.model_runtime.model_providers.openrouter.llm.llm import OpenRouterLargeLanguageModel
+
+
+def test_validate_credentials():
+ model = OpenRouterLargeLanguageModel()
+
+ with pytest.raises(CredentialsValidateFailedError):
+ model.validate_credentials(
+ model='mistralai/mixtral-8x7b-instruct',
+ credentials={
+ 'api_key': 'invalid_key',
+ 'mode': 'chat'
+ }
+ )
+
+ model.validate_credentials(
+ model='mistralai/mixtral-8x7b-instruct',
+ credentials={
+ 'api_key': os.environ.get('TOGETHER_API_KEY'),
+ 'mode': 'chat'
+ }
+ )
+
+
+def test_invoke_model():
+ model = OpenRouterLargeLanguageModel()
+
+ response = model.invoke(
+ model='mistralai/mixtral-8x7b-instruct',
+ credentials={
+ 'api_key': os.environ.get('TOGETHER_API_KEY'),
+ 'mode': 'completion'
+ },
+ prompt_messages=[
+ SystemPromptMessage(
+ content='You are a helpful AI assistant.',
+ ),
+ UserPromptMessage(
+ content='Who are you?'
+ )
+ ],
+ model_parameters={
+ 'temperature': 1.0,
+ 'top_k': 2,
+ 'top_p': 0.5,
+ },
+ stop=['How'],
+ stream=False,
+ user="abc-123"
+ )
+
+ assert isinstance(response, LLMResult)
+ assert len(response.message.content) > 0
+
+
+def test_invoke_stream_model():
+ model = OpenRouterLargeLanguageModel()
+
+ response = model.invoke(
+ model='mistralai/mixtral-8x7b-instruct',
+ credentials={
+ 'api_key': os.environ.get('TOGETHER_API_KEY'),
+ 'mode': 'chat'
+ },
+ prompt_messages=[
+ SystemPromptMessage(
+ content='You are a helpful AI assistant.',
+ ),
+ UserPromptMessage(
+ content='Who are you?'
+ )
+ ],
+ model_parameters={
+ 'temperature': 1.0,
+ 'top_k': 2,
+ 'top_p': 0.5,
+ },
+ stop=['How'],
+ stream=True,
+ user="abc-123"
+ )
+
+ assert isinstance(response, Generator)
+
+ for chunk in response:
+ assert isinstance(chunk, LLMResultChunk)
+ assert isinstance(chunk.delta, LLMResultChunkDelta)
+ assert isinstance(chunk.delta.message, AssistantPromptMessage)
+
+
+def test_get_num_tokens():
+ model = OpenRouterLargeLanguageModel()
+
+ num_tokens = model.get_num_tokens(
+ model='mistralai/mixtral-8x7b-instruct',
+ credentials={
+ 'api_key': os.environ.get('TOGETHER_API_KEY'),
+ },
+ prompt_messages=[
+ SystemPromptMessage(
+ content='You are a helpful AI assistant.',
+ ),
+ UserPromptMessage(
+ content='Hello World!'
+ )
+ ]
+ )
+
+ assert isinstance(num_tokens, int)
+ assert num_tokens == 21